How to bulk clean up system temporary files (language: C #, C + +, PHP, Python, Java) _php instances

Source: Internet
Author: User
Tags datetime echo date local time

Language controversy has a long history, the following do some IO experiment (traverse 9G files, bulk deletion), as far as possible with the facts to compare who superior who inferior. Operating system: Win7 64 bits, package size: 9.68G.

One, language: C #

Development environment: vs 2013

Total code: 43 lines

Time consuming: 7 seconds

Code:

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Linq;
Using System.Text;
Using System.Threading.Tasks;
Namespace Batchdelete
{
class program
{
static void Main (string[] args)
{
//input directory E:\tmp
string path;
Console.WriteLine ("Enter the directory to be cleaned up:");
Path = Console.ReadLine ();
Start Time
Console.WriteLine ("Start Time": "+datetime.now.tostring" ("HH:mm:ss"));
First traverse the matching lookup and then cycle delete
if (directory.exists (path))
{
Console.Write ("deleting");
foreach (String fileName in Directory.getfilesystementries (path))
{
if file.exists (filename) && Filename.contains ("Cachegrind.out"))
{
file.delete (fileName);
}
}
Console.WriteLine ("");
}
else
{
Console.WriteLine ("This directory does not exist!");
}
Timing over
Console.WriteLine ("End Time": "+ DateTime.Now.ToString" ("HH:mm:ss"));
Console.readkey ();}}

Run Effect chart:


Second, language: C + +

Development environment: vs 2013

Total Code: 50 lines

Time consuming: 36 seconds

Code:

#include <iostream> #include <string> #include <Windows.h> #include <boost\filesystem\ operations.hpp> #include <boost\filesystem\path.hpp> #include <boost\filesystem\convenience.hpp> #
Include <boost\algorithm\string.hpp> using namespace std; int main (int argc, char * argv[]) {//Enter directory e:\tmp string strpath; cout << "Enter the directory to be cleaned:" << Endl; Getline (CIN, S
Trpath); Start timing SYSTEMTIME sys_time; Declaring variable getlocaltime (&sys_time);
Set the variable value to local time printf ("Start timing:%02d:%02d:%02d\n", Sys_time.whour,sys_time.wminute,sys_time.wsecond);
First traversal matching lookup and then cycle Delete namespace fs = Boost::filesystem;
FS::p Ath Full_path (Fs::initial_path ());
Full_path = Fs::system_complete (fs::p Ath (strpath, fs::native)); if (fs::exists (Full_path)) {cout << "deleting"; FS::d irectory_iterator Item_begin (Full_path); FS::d Irectory_
Iterator item_end; for (; Item_begin!= item_end; item_begin++) {if (!fs::is_directory (*item_begin)) {if (fs::exists) (Item_begin->path () ) && BoOst::contains (Item_begin->path (). String (), "Cachegrind.out")) {Fs::remove (Item_begin->path ());}}
} cout << "" << Endl;}
else {cout << "This directory does not exist!" << Endl;}//Timing end getlocaltime (&sys_time);
printf ("Timed over:%02d:%02d:%02d\n", Sys_time.whour, Sys_time.wminute, Sys_time.wsecond);
System ("pause");
return 0; }

Run Effect chart:


Third, language: PHP

Development environment: Phpstorm

Total Code: 32 Lines

Time consuming: 13 seconds

Code:

<?php
/**
* Created by Phpstorm.
* User:administrator * date:16-1-29 * time
: Morning 9:31 * *
date_default_timezone_set (' PRC ');
Enter directory e:\tmp
$path = ' e:\tmp ';
Start Timing
echo Date ("H:i:s", Time ()). ' <br/> ';
First traverse the matching lookup and then cycle delete
if (Is_dir ($path))
{
echo "is deleting";
$mydir = Dir ($path);
while ($file = $mydir->read ())
{
if file_exists ("$path/$file") && Strpos ($file, ' cachegrind.out ') = = 0)
{
unlink ("$path/$file")
;
}
Echo ' <br/> ';
}
else
{
echo "This directory does not exist!". ' <br/> ';
}
Timing's over.

Run Effect chart:


Four, language: Java

Development environment: Eclipse

Total code: 43 lines

Time consuming: 10 seconds

Code:

Package com.yejing;
Import Java.io.File;
Import Java.text.SimpleDateFormat;
Import java.util.Date;
Import Java.util.Scanner;
public class Test {public
static void Main (string[] args) {
Scanner s = new Scanner (system.in);
Enter directory e:\tmp
String path = null;
System.out.println ("Enter the directory to be cleaned up:");
Path = S.next ();
Start timing
Date nowtime=new date (); 
SimpleDateFormat time=new SimpleDateFormat ("HH:mm:ss"); 
System.out.println ("Start time:" + Time.format (nowtime)); 
First traversal matching lookup and then looping delete
file dir = new file (path);
if (dir.exists ()) {
System.out.print ("deleting");
file[] fs = Dir.listfiles ();
for (int i=0;i<fs.length;i++) {
if (!fs[i].isdirectory ()) {
if (fs[i].isfile () && fs[i].exists () && fs[i].getname (). Contains ("Cachegrind.out"))
{
fs[i].delete ();
}}} System.out.println ("");
} else{
System.out.println ("This directory does not exist!")
;
Timing End
nowtime=new Date (); 
System.out.println ("Start timing:" + time.format (nowtime)); 
}

Run Effect chart:

V. Language: Python 3.3.5

Development environment: IDLE

Total Code: 20 lines

Time consuming: 10 seconds

Code:

#-*-Coding:utf-8-*- 
import datetime
import OS
# Enter directory e:\tmp
path = input ("Enter the directory to be cleaned: \ n");
# Start Timing
print ("Start timing:", Datetime.datetime.now (). Strftime ('%h:%m:%s '));
# First traversal match lookup and then cycle delete
if (os.path.exists (path)):
print ("deleting");
For parent,dirnames,filenames in Os.walk (path):
for filename in filenames:
TargetFile = os.path.join (parent, FileName)
if (Os.path.isfile (targetfile) and "Cachegrind.out" in TargetFile):
os.remove (targetfile)

Else

Print ("This directory does not exist!");
# Time's over.

Run Effect chart:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.