Contrast Experiment (1)-Batch cleanup of temporary system files and comparison experiment cleanup
Language competition has been around for a long time. I will do some I/O experiments (traverse more than 9 GB files and delete them in batches) below to compare who is better and who is worse with the facts. OS: win7 64-bit, file package size: 9.68 GB.
I. Language: C #
Development Environment: V. S. 2013
Total number of lines of code: 43
Time consumed: 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 cleared:"); path = Console. readLine (); // start the timer Console. writeLine ("Start Time:" + DateTime. now. toString ("HH: mm: ss"); // traverse and match to find and recycle and 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! ");} // Time end Console. writeLine ("End Time:" + DateTime. now. toString ("HH: mm: ss"); Console. readKey ();}}}
Run:
2. Language: C/C ++
Development Environment: V. S. 2013
Total number of lines of code: 50
Time consumed: 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 []) {// input directory e: \ tmp string strPath; cout <"Enter the directory to be cleansed: "<endl; getline (cin, strPath); // start timing SYSTEMTIME sys_time; // declare the variable GetLocalTime (& Sys_time); // set the variable value to the local time printf ("Start Time: % 02d: % 02d: % 02d \ n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond ); // traverse and match search first, find and recycle and delete namespace fs = boost: filesystem; fs: path full_path (fs: initial_path (); full_path = fs: system_complete (fs :: path (strPath, fs: native); if (fs: exists (full_path) {cout <"deleting"; fs: directory_iterator item_begin (full_path ); fs: directory_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 ;}// time end GetLocalTime (& sys_time); printf (" time end: % 02d: % 02d: % 02d \ n ", sys_time.wHour, sys_time.wMinute, sys_time.wSecond); system ("pause"); return 0 ;}
Run:
Iii. Language: PHP
Development Environment: Phpstorm
Total number of lines of code: 32
Time consumed: 13 seconds
Code:
<? Php/*** Created by PhpStorm. * User: Administrator * Date: 16-1-29 * Time: */date_default_timezone_set ('prc'); // enter the directory e: \ tmp $ path = 'e: \ tmp '; // start timing echo date ("H: I: s", time ()). '<br/>'; // traverse and match search first. Recycle and delete if (is_dir ($ path) {echo "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/>';} // time end echo date (" H: I: s ", time (). '<br/> ';
Run:
4. Language: Java
Development Environment: eclipse
Total number of lines of code: 43
Time consumed: 10 seconds
Code:
Package com. yejing; import java. io. file; import java. text. simpleDateFormat; import java. util. date; import java. util. role; public class Test {public static void main (String [] args) {s = new role (System. in); // input directory e: \ tmpString path = null; System. out. println ("Enter the directory to be cleaned:"); path = s. next (); // start timing Date nowTime = new Date (); SimpleDateFormat time = new SimpleDateFormat ("HH: mm: ss"); System. out. println ("Start timing:" + time. format (nowTime); // first traverse and match to find and recycle 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! ");} // End of time nowTime = new Date (); System. out. println (" Start time: "+ time. format (nowTime ));}}
Run:
V. Language: Python 3.3.5
Development Environment: IDLE
Total number of lines of code: 20
Time consumed: 10 seconds
Code:
#-*-Coding: UTF-8-*-import datetimeimport OS # input directory e: \ tmppath = input ("input directory to be cleaned: \ n "); # Start time print ("Start Time:", datetime. datetime. now (). strftime ('% H: % M: % s'); # traverse and match to find and recycle and 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. r Emove (targetFile) else: print ("this directory does not exist! "); # Print (" End Time: ", datetime. datetime. now (). strftime ('% H: % M: % s '));
Run: