Qt vs Boost 文件系统递归文件计数
Qt vs Boost filesystem recursive file count
void countFiles() {
QString root_path("C:\");
QTime timer;
timer.start();
std::uint64_t count = 0;
std::queue<QString> qt_dirs;
qt_dirs.push(root_path);
while (!qt_dirs.empty()) {
auto dir_path = qt_dirs.front();
qt_dirs.pop();
QDir dir(dir_path);
count += dir.entryList(QDir::Dirs | QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot).size();
for (auto &sub_dir_path : dir.entryList(QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot)) {
qt_dirs.push(dir.filePath(sub_dir_path));
}
}
qDebug() << Q_FUNC_INFO << "found" << count << "entries, and it took" << timer.elapsed() << "ms";
timer.start();
count = 0;
std::queue<boost::filesystem::path> dirs;
dirs.push(root_path.toStdString());
while (!dirs.empty()) {
auto dir_path = dirs.front();
dirs.pop();
try {
auto iterator_range = boost::make_iterator_range(boost::filesystem::directory_iterator(dir_path), {});
for (auto &entry : iterator_range) {
auto entry_status = entry.status();
if (boost::filesystem::is_symlink(entry_status)) continue;
if (boost::filesystem::is_directory(entry_status)) dirs.push(entry.path());
++count;
}
} catch(boost::filesystem::filesystem_error &fe) {
continue;
}
}
qDebug() << Q_FUNC_INFO << "found" << count << "entries, and it took" << timer.elapsed() << "ms";
}
有人可以向我解释一下,或者至少给我一个提示,为什么这两个块返回完全不同的文件数?它们都应该只计算目录和文件,跳过任何符号链接。但是,在Windows上,这仍然相差约20%。
void VolumeFileTreeModel::countFiles() found 502780 entries, and it took 97549 ms
void VolumeFileTreeModel::countFiles() found 622208 entries, and it took 17022 ms
一个显着的区别是
QDir::NoDotAndDotDot
过滤器。您可以尝试修改 Boost 版本以跳过这些目录(我想将目录名称与 ..
和 .
进行比较应该没问题?从现在开始,我想它们将被 Boost 而不是 Qt 计算在内。
更新
我会再试一次 - 添加 Qt 过滤器QDir::Hidden
和QDir::System
怎么样?正如我从[这个]问题中了解到的那样,至少隐藏文件包含在Boost中。
相关文章:
- 递归计数给定目录的文件和所有目录
- 包含模板文件的递归会导致编译失败
- 递归地将FEATURE_FLAG导出到生成文件、CPP 和头文件
- 如何递归复制文件和目录
- 使用FindFirstFile和FindNextFile递归查找具有特定扩展名的文件
- boost::文件系统递归获取每个文件的大小
- C++ 源代码的递归生成文件
- 问题 - 递归函数以返回文本文件排列
- 使用C 列出目录中的文件,而不是递归,只有文件和无子目录
- 列出MFC错误中递归运行的所有文件
- 同一生成文件中的 make 目标之间的递归依赖关系
- 递归递归文件
- 递归扫描目录中的文件
- 使用boost递归地将给定目录中每个最低级别文件夹的文件复制到另一个目录-C++
- 输入所有子文件夹 - 递归
- 用于列出子目录中所有文件的递归功能
- 使用递归获取某个目录中的所有文件
- Qt vs Boost 文件系统递归文件计数
- 如何用C++递归地从二进制文件中读取自定义字符串
- 在Qt中递归浏览目录,跳过文件夹"."并".."