Last active
November 12, 2023 13:41
-
-
Save mmatyas/fec115b7fcb527133fb35e1b82355b6a to your computer and use it in GitHub Desktop.
For Pegasus issue #1089
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Based on https://gist.github.com/Motherboard/9bce170327140a709dd40ac23bd863fa | |
#include <QDirIterator> | |
#include <chrono> | |
#include <filesystem> | |
#include <iostream> | |
#include <functional> | |
#include <dirent.h> | |
#define TEST_PATH "/tmp/Arcade" | |
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; | |
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_dirent() { | |
using namespace std::chrono; | |
int num_files = 0; | |
auto start_time = high_resolution_clock::now(); | |
DIR* dir = opendir(TEST_PATH); | |
for (struct dirent* entry = readdir(dir); entry != NULL; entry = readdir(dir)) { | |
const QString name(entry->d_name); | |
num_files++; | |
} | |
closedir(dir); | |
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) << std::endl; | |
std::cout << "c++17 directory_iterator: " << loop_test(get_time_for_ts) << std::endl; | |
std::cout << "classic dirent: " << loop_test(get_time_for_dirent) << std::endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment