Skip to content

Instantly share code, notes, and snippets.

@poseidon4o
Created March 3, 2015 16:50
Show Gist options
  • Save poseidon4o/b2a802024e820ff9fb30 to your computer and use it in GitHub Desktop.
Save poseidon4o/b2a802024e820ff9fb30 to your computer and use it in GitHub Desktop.
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <chrono>
#include <limits>
#include <functional>
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
using namespace std::chrono;
template <typename T>
void time_me(T fn, string message = "") {
auto s = high_resolution_clock::now();
fn();
auto e = high_resolution_clock::now();
cout << message << duration_cast<seconds>(e - s).count() << endl;
}
void make_file() {
time_me([] {
ofstream file("test.dat");
for (uint32_t c = 0; c < 1000000000; ++c) {
file << c;
}
file.close();
});
}
void ios_read() {
time_me([] {
ifstream file("test.dat");
uint32_t d, x = 0;
while (file >> d) {
x ^= d;
}
}, "read ");
}
void ios_seek_read() {
time_me([]{
ifstream file("test.dat");
uint32_t d, x = 0;
while (file >> d) {
file.seekg(static_cast<int>(file.tellg()) + 100, ios::beg);
x ^= d;
}
}, "seek-read ");
}
void stdio_read() {
time_me([] {
FILE * f = fopen("test.dat", "r");
uint32_t x, d = 0;
while (fread(&x, sizeof(x), 1, f)) {
d ^= x;
}
fclose(f);
}, "stdio-read ");
}
void stdio_read_buff() {
const int sz = 2048;
uint32_t buff[sz];
time_me([&buff, sz] {
FILE * f = fopen("test.dat", "r");
setbuf(f, NULL);
uint32_t x, d = 0;
int rd = 0;
while (rd = fread(buff, sizeof(buff[0]), sz, f)) {
for (int c = 0; c < rd; ++c) {
d ^= buff[c];
}
}
fclose(f);
}, "stdio-manual-buff ");
}
int main() {
ios_read();
ios_seek_read();
stdio_read();
stdio_read_buff();
cin.get();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment