The language debate has been around for a long time, let’s do some IO experiments below (traversing more than 9G files, batch deletion), try to use facts to compare who is better and who is worse. Operating system: win7 64-bit, File package size: 9.68G.
1. Language: C#
Development environment: vs 2013
Total number of lines of code: 43 lines
Time taken: 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) { // 输入目录 e:\tmp string path; Console.WriteLine("输入要清理的目录:"); path = Console.ReadLine(); // 开始计时 Console.WriteLine("开始计时:"+DateTime.Now.ToString("HH:mm:ss")); // 先遍历匹配查找再循环删除 if (Directory.Exists(path)) { Console.Write("正在删除"); foreach (string fileName in Directory.GetFileSystemEntries(path)) { if (File.Exists(fileName) && fileName.Contains("cachegrind.out")) { File.Delete(fileName); } } Console.WriteLine(""); } else { Console.WriteLine("该目录不存在!"); } // 计时结束 Console.WriteLine("结束计时:" + DateTime.Now.ToString("HH:mm:ss")); Console.ReadKey(); } } }
Operation rendering:
2. Language: C/C
Development environment: vs 2013
Total number of lines of code: 50 lines
Time taken: 36 seconds
Code:
#include <iostream><span> #include </span><<span>string</span>><span> #include </span><Windows.h><span> #include </span><boost\filesystem\operations.hpp><span> #include </span><boost\filesystem\path.hpp><span> #include </span><boost\filesystem\convenience.hpp><span> #include </span><boost\algorithm\<span>string</span>.hpp> <span>using</span> <span>namespace</span><span> std; </span><span>int</span> main(<span>int</span> argc, <span>char</span> *<span> argv[]) { </span><span>//</span><span> 输入目录 e:\tmp</span> <span>string</span><span> strPath; cout </span><< <span>"</span><span>输入要清理的目录:</span><span>"</span> <<<span> endl; getline(cin, strPath); </span><span>//</span><span> 开始计时 </span> SYSTEMTIME sys_time; <span>//</span><span>声明变量</span> GetLocalTime(&sys_time); <span>//</span><span>将变量值设置为本地时间</span> printf(<span>"</span><span>开始计时:%02d:%02d:%02d\n</span><span>"</span><span>, sys_time.wHour,sys_time.wMinute,sys_time.wSecond); </span><span>//</span><span> 先遍历匹配查找再循环删除</span> <span>namespace</span> fs =<span> boost::filesystem; fs::path full_path(fs::initial_path()); full_path </span>=<span> fs::system_complete(fs::path(strPath, fs::native)); </span><span>if</span><span> (fs::exists(full_path)) { cout </span><< <span>"</span><span>正在删除</span><span>"</span><span> ; fs::directory_iterator item_begin(full_path); fs::directory_iterator item_end; </span><span>for</span> (; item_begin != item_end; item_begin++<span>) { </span><span>if</span> (!fs::is_directory(*<span>item_begin)) { </span><span>if</span> (fs::exists(item_begin->path()) && boost::contains(item_begin->path().<span>string</span>(), <span>"</span><span>cachegrind.out</span><span>"</span><span>)) { fs::remove(item_begin</span>-><span>path()); } } } cout </span><< <span>""</span> <<<span> endl; } </span><span>else</span><span> { cout </span><< <span>"</span><span>该目录不存在!</span><span>"</span> <<<span> endl; } </span><span>//</span><span> 计时结束</span> GetLocalTime(&<span>sys_time); printf(</span><span>"</span><span>计时结束:%02d:%02d:%02d\n</span><span>"</span><span>, sys_time.wHour, sys_time.wMinute, sys_time.wSecond); system(</span><span>"</span><span>pause</span><span>"</span><span>); </span><span>return</span> <span>0</span><span>; }</span>
Operation rendering:
3. Language: PHP
Development environment: Phpstorm
Total number of lines of code: 32 lines
Time taken: 13 seconds
Code:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 16-1-29 * Time: 上午9:31 */ date_default_timezone_set('prc'); //输入目录 e:\tmp $path = 'e:\tmp'; //开始计时 echo date("H:i:s",time()) . '<br/>'; //先遍历匹配查找再循环删除 if(is_dir($path)) { echo "正在删除"; $mydir = dir($path); while($file = $mydir->read()) { if(file_exists("$path/$file") && strpos($file, 'cachegrind.out') === 0) { unlink("$path/$file"); } } echo '<br/>'; } else { echo "该目录不存在!" . '<br/>'; } //计时结束 echo date("H:i:s",time()) . '<br/>';
Operation rendering:
4. Language: Java
Development environment: eclipse
Total lines of 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); // 输入目录 e:\tmp String path = null; System.out.println("输入要清理的目录:"); path = s.next(); // 开始计时 Date nowTime=new Date(); SimpleDateFormat time=new SimpleDateFormat("HH:mm:ss"); System.out.println("开始计时:"+ time.format(nowTime)); // 先遍历匹配查找再循环删除 File dir = new File(path); if(dir.exists()){ System.out.print("正在删除"); 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("该目录不存在!"); } // 计时结束 nowTime=new Date(); System.out.println("开始计时:"+ time.format(nowTime)); } }
Operation rendering:
5. Language: Python 3.3.5
Development environment: IDLE
Total number of lines of code: 20 lines
Time consuming: 10 seconds
Code:
# -*- coding: utf-8 -*- import datetime import os # 输入目录 e:\tmp path = input("输入要清理的目录:\n"); # 开始计时 print("开始计时:",datetime.datetime.now().strftime('%H:%M:%S')); # 先遍历匹配查找再循环删除 if(os.path.exists(path)): print("正在删除"); 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("该目录不存在!"); # 计时结束 print("结束计时:",datetime.datetime.now().strftime('%H:%M:%S'));
Operation rendering: