Last active
August 29, 2015 14:01
-
-
Save Rush/d700782dac56073682bd to your computer and use it in GitHub Desktop.
C++ threaded read test
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
#include <thread> | |
#include <fstream> | |
#include <vector> | |
#include <chrono> | |
#include <sys/stat.h> | |
#include <iostream> | |
#include <atomic> | |
long file_size(const char* file) | |
{ | |
struct stat stat_buf; | |
int rc = stat(file, &stat_buf); | |
return rc == 0 ? stat_buf.st_size : -1; | |
} | |
int main(int argc, char** argv) | |
{ | |
system("echo 3 > /proc/sys/vm/drop_caches"); | |
long total_size = 0; | |
auto thread_for_file = [&](const char* file) { | |
total_size += file_size(file); | |
return [=] { | |
std::ifstream f; | |
f.open(file); | |
char buffer[2<<19]; | |
while(f.read(buffer, sizeof(buffer))); | |
}; | |
}; | |
auto timer1 = std::chrono::high_resolution_clock::now(); | |
std::vector<std::thread> threads; | |
for(int i = 1; i < argc;++i) { | |
threads.emplace_back(std::thread(thread_for_file(argv[i]))); | |
} | |
for(auto &thread: threads) { | |
thread.join(); | |
} | |
auto timer2 = std::chrono::high_resolution_clock::now(); | |
auto total_ms = std::chrono::duration_cast<std::chrono::milliseconds>(timer2 - timer1); | |
double MBs = (((double)total_size)/1024/1024); | |
double s = ((double)total_ms.count()/1000); | |
std::cout << "Read " << MBs << " MBs in " << s << "s (" << (MBs/s) << " MB/s)\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment