-
-
Save anonymous/5e22973ebfa0f06c5215 to your computer and use it in GitHub Desktop.
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 <cmath> | |
#include <cstdio> | |
#include <fstream> | |
#include <iomanip> | |
#include <iostream> | |
#include <nonius/main.h++> | |
struct CRead | |
{ | |
CRead(char const* filename) : _filename(filename) {} | |
void operator()() | |
{ | |
FILE* file = fopen(_filename, "r"); | |
int count = 0; | |
while (fscanf(file, "%s", _buffer) == 1) { ++count; } | |
fclose(file); | |
} | |
char const* _filename; | |
char _buffer[1024]; | |
}; | |
NONIUS_BENCHMARK("C", [](nonius::chronometer meter) | |
{ | |
CRead reader("big.txt"); | |
meter.measure([&]() { reader(); }); | |
}) | |
struct CppRead | |
{ | |
CppRead(char const* filename) : _filename(filename), _buffer() {} | |
enum { BufferSize = 16184 }; | |
void operator()() | |
{ | |
std::ifstream file(_filename, std::ifstream::in); | |
// comment to remove extended buffer | |
file.rdbuf()->pubsetbuf(_buffer, BufferSize); | |
int count = 0; | |
std::string s; | |
while (file >> s) { ++count; } | |
} | |
char const* _filename; | |
char _buffer[BufferSize]; | |
}; | |
NONIUS_BENCHMARK("C++", [](nonius::chronometer meter) | |
{ | |
CppRead reader("big.txt"); | |
bool oldSyncSetting = std::ios_base::sync_with_stdio(false); | |
meter.measure([&]() { reader(); }); | |
std::ios_base::sync_with_stdio(oldSyncSetting); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment