【发布时间】:2010-06-14 22:11:35
【问题描述】:
我编写了一个小型爬虫来扫描和处理目录结构。
它基于 dirent(它是 FindNextFileA 的一个小包装器) 在我的第一个基准测试中,它出奇地慢:
4500 个文件大约需要 123473 毫秒(thinkpad t60p 本地三星 320 GB 2.5" HD)。 在 123473 毫秒内找到 121481 个文件 这个速度正常吗?
这是我的代码:
int testPrintDir(std::string strDir, std::string strPattern="*", bool recurse=true){
struct dirent *ent;
DIR *dir;
dir = opendir (strDir.c_str());
int retVal = 0;
if (dir != NULL) {
while ((ent = readdir (dir)) != NULL) {
if (strcmp(ent->d_name, ".") !=0 && strcmp(ent->d_name, "..") !=0){
std::string strFullName = strDir +"\\"+std::string(ent->d_name);
std::string strType = "N/A";
bool isDir = (ent->data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=0;
strType = (isDir)?"DIR":"FILE";
if ((!isDir)){
//printf ("%s <%s>\n", strFullName.c_str(),strType.c_str());//ent->d_name);
retVal++;
}
if (isDir && recurse){
retVal += testPrintDir(strFullName, strPattern, recurse);
}
}
}
closedir (dir);
return retVal;
} else {
/* could not open directory */
perror ("DIR NOT FOUND!");
return -1;
}
}
【问题讨论】:
-
如果你觉得你可以运行我的 SizeReporter 工具 [color-of-code.de/… 它只不过是爬取目录树并检查大小和时间戳并报告它们。如果速度快得多,那么它可能在于一些日志记录,因为我使用的代码与你的非常相似。
标签: c++ performance recursion directory ntfs