【问题标题】:Qt vs Boost filesystem recursive file countQt vs Boost文件系统递归文件计数
【发布时间】:2015-04-30 13:59:17
【问题描述】:
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

【问题讨论】:

  • 添加一些打印语句,打印出正在计数的文件和目录,并比较输出。
  • 投反对票的人能否也解释一下这个问题有什么问题? ..谢谢你,我会尝试区分输出

标签: c++ qt boost boost-filesystem


【解决方案1】:

一个显着的区别是QDir::NoDotAndDotDot 过滤器。您是否可以尝试修改 Boost 版本以跳过这些目录(我想将目录名称与 ... 进行比较应该没问题?因为现在我猜它们将被 Boost 而不是 Qt 计算在内。

更新

我会再试一次 - 添加 Qt 过滤器QDir::HiddenQDir::System 怎么样?据我了解 [this] question 至少隐藏文件包含在 Boost 中。

【讨论】:

  • boost.org/doc/libs/1_46_1/libs/filesystem/v3/doc/… 的 boost 文档说 “目录迭代不应产生当前(点)和父(点)目录的目录条目。”(以及代码如果确实如此,肯定永远不会终止。)
  • 是的,我认为它应该触发无限递归:) 我将删除答案,因为它没用。
  • @RichieHindle 因为这个问题让我很好奇,我会再尝试一个建议:D
猜你喜欢
  • 1970-01-01
  • 2018-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多