PDA

View Full Version : Fast solution to list directories recursively



stereoMatching
2nd February 2014, 11:58
Do you know a faster solution to find all of the directories by Qt(QDirIterator is too slow)



QDirIterator directories(dir_, QDir::Dirs | QDir::NoSymLinks | QDir::NoDotAndDotDot, QDirIterator::Subdirectories);
QStringList dirs;
while(directories.hasNext()){
directories.next();
dirs.push_back(directories.filePath());
}





//codes base on BFS(breadth first search)
QStringList scan_dir_recursive(QDir const &dir)
{
std::queue<QString> queue;
queue.push(dir.absolutePath());
QStringList results;
while (!queue.empty()) {
auto const subdir_name = queue.front();
queue.pop();
results.push_back(subdir_name);
QDir subdir(subdir_name);

auto const &directories = subdir.entryInfoList({},
QDir::AllDirs | QDir::NoSymLinks | QDir::NoDotAndDotDot);
for (auto const &data : directories) {
queue.push(data.absoluteFilePath());
}
}

return results;
}

jho
27th July 2014, 16:47
I dont know where to post, so I just use your post.

I found out that "QDir::NoSymLinks" is very slow. I compared it with the windows dir command. Now both methods runs in a similar speed. Both needed 3 seconds to run through the QT folder. 33000 files in 1100 folders.

With QDir::NoSymLinks on, it took 11 seconds.

I have to run my tool on a network and it is around 100 times slower than the dir command method.


my used code is quite simple:



StringList files;
QDirIterator it(source , QDir::Files , QDirIterator::Subdirectories); //QDir::Files | QDir::NoSymLinks | QDir::NoDotAndDotDot

while (it.hasNext())
{

files.append(it.next());
}