多语言批量清理临时文件性能对比实验
编程语言之间的优劣之争由来已久,为了探究真相,这里进行了一系列关于IO操作的实验,主要任务是对超过9GB的文件进行遍历并批量删除,力求以客观数据来评判各语言的效能高低。测试平台为Windows 7 64位操作系统,待处理文件包总大小约为9.68GB。
第一部分测试对象为C#语言。
使用的开发环境是Visual Studio 2013。
该实现方案的代码总行数为43行。
整个执行过程耗时7秒。
具体代码如下:
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();
}
}
}
程序运行后的效果截图如下:

第二部分测试对象为C/C++语言。
同样使用Visual Studio 2013作为开发环境。
该实现方案的代码总行数为50行。
整个执行过程耗时36秒。
具体代码如下:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
int main(int argc, char * argv[])
{
// 输入目录 e:\tmp
string strPath;
cout << "输入要清理的目录:" << endl;
getline(cin, strPath);
// 开始计时
SYSTEMTIME sys_time; //声明变量
GetLocalTime(&sys_time); //将变量值设置为本地时间
printf("开始计时:%02d:%02d:%02d\n", sys_time.wHour,sys_time.wMinute,sys_time.wSecond);
// 先遍历匹配查找再循环删除
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 << "正在删除" ;
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 << "该目录不存在!" << endl;
}
// 计时结束
GetLocalTime(&sys_time);
printf("计时结束:%02d:%02d:%02d\n", sys_time.wHour, sys_time.wMinute, sys_time.wSecond);
system("pause");
return 0;
}
程序运行后的效果截图如下:

第三部分测试对象为PHP语言。
使用的开发工具是Phpstorm。
该实现方案的代码总行数为32行。
整个执行过程耗时13秒。
具体代码如下:
<?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()) . '
';
//先遍历匹配查找再循环删除
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 '
';
}
else
{
echo "该目录不存在!" . '
';
}
//计时结束
echo date("H:i:s",time()) . '
';
程序运行后的效果截图如下:

第四部分测试对象为Java语言。
使用的开发环境是Eclipse。
该实现方案的代码总行数为43行。
整个执行过程耗时10秒。
具体代码如下:
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
程序运行后的效果截图如下:

第五部分测试对象为Python 3.3.5版本。
使用的开发环境是IDLE。
该实现方案的代码总行数为20行。
整个执行过程耗时10秒。
具体代码如下:
# -*- 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'));
程序运行后的效果截图如下:
