Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2014 14:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/5e22973ebfa0f06c5215 to your computer and use it in GitHub Desktop.
Save anonymous/5e22973ebfa0f06c5215 to your computer and use it in GitHub Desktop.
#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