I just wrote a C++ code to list all the directories in a folder recursively. I'm using Boost Filesystem and I have build the following code statically:
#include <iostream>
#include <boost/filesystem.hpp>
using namespace boost::filesystem;
using namespace std;
int main(int argc, char* argv[]) {
const path current_file_path(current_path());
try {
std::vector<path> directories_vector;
for (auto&& x : recursive_directory_iterator(current_file_path))
if (is_directory(x.path()))
directories_vector.push_back(x.path());
/* GETTING UP TO HERE TAKES MORE TIME THAN PYTHON OR RUBY*/
for (auto&& x : directories_vector) {
cout << x << '\n';
}
}
catch (const filesystem_error& ex) {
cout << ex.what() << '\n';
}
cin.get();
return 0;
}
I wanted to see how fast this code would work against Python & Ruby. I know I/O related stuff are not good for evaluating code performance but when I run the C++ executable, it takes nearly 3 seconds for 15+ recursive folders while the following Python & Ruby codes are run nearly instantly:
Ruby:
Dir.glob("**/*/")
Python:
[x[0] for x in os.walk(directory)]
All of the codes are running on an SSD. I'm using Visual Studio 2017, Python 3.5.2 and Ruby 2.4 on Windows. The C++ code is using Release/x64 mode and Optimization is set to Maximum Optimization (Favor Speed) (/O2).
Why is the C++ code slower when faced with lots of recursive folders?