Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Motherboard/9bce170327140a709dd40ac23bd863fa to your computer and use it in GitHub Desktop.
Save Motherboard/9bce170327140a709dd40ac23bd863fa to your computer and use it in GitHub Desktop.
Compare boost directory_iterator and QDirIterator
#include <QDirIterator>
#include <chrono>
#include <experimental/filesystem>
#include <boost/filesystem.hpp>
#include <iostream>
#include <functional>
#define TEST_PATH "/home/master/test"
unsigned long long get_time_for_qiter() {
using namespace std::chrono;
QDirIterator qiter(TEST_PATH);
int num_files = 0;
auto start_time = high_resolution_clock::now();
while (qiter.hasNext()) {
num_files++;
qiter.next();
}
return (high_resolution_clock::now() - start_time).count();
}
unsigned long long get_time_for_ts() {
using namespace std::chrono;
using namespace std::experimental;
int num_files = 0;
auto start_time = high_resolution_clock::now();
for (auto &_: filesystem::directory_iterator(TEST_PATH)) {
num_files++;
}
return (high_resolution_clock::now() - start_time).count();
}
unsigned long long get_time_for_boost() {
using namespace std::chrono;
using namespace boost;
int num_files = 0;
auto start_time = high_resolution_clock::now();
for (auto &_: filesystem::directory_iterator(TEST_PATH)) {
num_files++;
}
return (high_resolution_clock::now() - start_time).count();
}
unsigned long long loop_test(std::function<double()> func) {
unsigned long long time_sum = 0;
for (int i=0; i < 10; ++i)
time_sum += func();
return time_sum;
}
int main(int argc, char *argv[])
{
std::cout << "QDirIterator: " << loop_test(get_time_for_qiter) << " sesonds" << std::endl;
std::cout << "boost directory_iterator: " << loop_test(get_time_for_boost) << " sesonds" << std::endl;
std::cout << "c++17 directory_iterator: " << loop_test(get_time_for_ts) << " sesonds" << std::endl;
}
@Motherboard
Copy link
Author

the test dir contains 1M files generated using the following script:

#!/usr/bin/env python

if __name__ == "__main__":
        for i in range(1000000):
                with open('%s.tmp' % hash(str(i)), 'w'):
                        continue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment