问题
I am currently in the process of learning the Boost framework, and I have found out how to list all folders and files on my system, using
#include <boost/filesystem.hpp>
#include <boost/foreach.hpp>
#include <iostream>
using namespace std;
int main()
{
for ( boost::filesystem::recursive_directory_iterator end, dir("C:\\");
dir != end; ++dir ) {
cout << *dir << std::endl;
}
return 0;
}
but the only problem I'm having is how slow the process is... am I doing something wrong or is it just that Microsoft's .NET version of listing all files is much faster? Thanks!
回答1:
Your question implies a comparison, but you've only provided half the information i.e. where is the code to which you are making the comparison? There are many ways you can increase the performance of the code you have supplied, some of which have been supplied in the comments above.
That said, it's likely that the reason you are observing a performance difference in the first place can probably be traced to the very managed environment on top of which the C# codes runs. It's quite likely that your filesystem is indexed in .Net's memory space while your C++ code and the Boost library are going directly to the filesystem and not benefitting from one of the ways Microsoft has tried to make the .NET environment more efficient. Without the efficiencies, it seems more likely that C# code would be orders of magnatude slower than compiled C++ code of the same quality.
回答2:
It also depends how many files are present in the folder. If there are many files, then it does take a lot of time. Did you try with a folder containing very few files?
回答3:
The .NET version might be indexed and perhaps only needs to read filenames from a flat format. What you propose needs to open every single directory.
来源:https://stackoverflow.com/questions/13897401/boost-filesystem-incredibly-slow