Skip to content

Instantly share code, notes, and snippets.

@yatsuta
Created December 3, 2011 16:14
Show Gist options
  • Save yatsuta/1427474 to your computer and use it in GitHub Desktop.
Save yatsuta/1427474 to your computer and use it in GitHub Desktop.
performance check for getline with various buffer sizes
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
// usage:
// time ./a.out [1G|1M|default] filename > /dev/null
void error(const char* p, const char* p2 = "")
{
std::cerr << p << ' ' << p2 << '\n';
std::exit(1);
}
int main(int argc, char* argv[])
{
if (argc != 3) error ("wrong number of arguments");
std::ifstream from(argv[2]);
if (!from) error("cannot open input file", argv[2]);
const int M = 1024 * 1024;
const int G = 1024 * 1024 * 1024;
char* internal_buf;
char* buf;
int size = 4096;
if (std::string(argv[1]) == std::string("1M")) {
size = M;
} else if (std::string(argv[1]) == std::string("1G")) {
size = G;
}
internal_buf = new char[size];
buf = new char[size];
from.rdbuf()->pubsetbuf(internal_buf, size);
std::string s;
while (!from.eof()) {
from.read(buf, size);
// std::getline(from, s);
// std::cout << << std::endl;
}
if (!from.eof()) error("something strange happend");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment