Created
March 7, 2025 00:47
-
-
Save fearofcode/240d735a2b26e3ad5f9ab314a464bf07 to your computer and use it in GitHub Desktop.
either this benchmark is fucked or C++ style I/O is slow as shit
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 <iostream> | |
#include <chrono> | |
#include <vector> | |
#include <fstream> | |
#include <string> | |
#include <cstdlib> | |
#include <cstdio> | |
using namespace std; | |
using namespace std::chrono; | |
// Function to generate test data file with one integer per line | |
void generate_test_data(const string& filename, int n) { | |
ofstream file(filename); | |
for (int i = 0; i < n; i++) { | |
file << i << "\n"; // One integer per line | |
} | |
file.close(); | |
} | |
// Benchmark functions that directly read from stdin | |
// Each version is compiled separately to ensure clean test environment | |
// Standard C++ I/O | |
int benchmark_standard() { | |
auto start = high_resolution_clock::now(); | |
vector<int> numbers; | |
int x; | |
while (cin >> x) { | |
numbers.push_back(x); | |
} | |
auto end = high_resolution_clock::now(); | |
auto duration = duration_cast<microseconds>(end - start); | |
cout << "Standard I/O: Read " << numbers.size() << " numbers in " | |
<< duration.count() << " microseconds" << endl; | |
return 0; | |
} | |
// Optimized C++ I/O | |
int benchmark_optimized() { | |
// Optimized settings | |
ios::sync_with_stdio(false); | |
cin.tie(nullptr); | |
auto start = high_resolution_clock::now(); | |
vector<int> numbers; | |
int x; | |
while (cin >> x) { | |
numbers.push_back(x); | |
} | |
auto end = high_resolution_clock::now(); | |
auto duration = duration_cast<microseconds>(end - start); | |
cout << "Optimized I/O: Read " << numbers.size() << " numbers in " | |
<< duration.count() << " microseconds" << endl; | |
return 0; | |
} | |
// C-style I/O | |
int benchmark_c_style() { | |
auto start = high_resolution_clock::now(); | |
vector<int> numbers; | |
int x; | |
while (scanf("%d", &x) == 1) { | |
numbers.push_back(x); | |
} | |
auto end = high_resolution_clock::now(); | |
auto duration = duration_cast<microseconds>(end - start); | |
printf("C-style I/O: Read %zu numbers in %lld microseconds\n", | |
numbers.size(), (long long)duration.count()); | |
return 0; | |
} | |
// Main function to generate data and compile separate benchmarks | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
cerr << "Usage: " << argv[0] << " [generate|standard|optimized|c_style]" << endl; | |
return 1; | |
} | |
string mode = argv[1]; | |
if (mode == "generate") { | |
const int n = 1000000; // 1 million numbers | |
const string filename = "test_data.txt"; | |
cout << "Generating test data with " << n << " integers (one per line)..." << endl; | |
generate_test_data(filename, n); | |
} | |
else if (mode == "standard") { | |
return benchmark_standard(); | |
} | |
else if (mode == "optimized") { | |
return benchmark_optimized(); | |
} | |
else if (mode == "c_style") { | |
return benchmark_c_style(); | |
} | |
else { | |
cerr << "Unknown mode: " << mode << endl; | |
return 1; | |
} | |
return 0; | |
} |
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
wkh@mayhem ~/c/sync_with_stdio_benchmark> wc -l test_data.txt; head test_data.txt; tail test_data.txt | |
1000000 test_data.txt | |
0 | |
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
999990 | |
999991 | |
999992 | |
999993 | |
999994 | |
999995 | |
999996 | |
999997 | |
999998 | |
999999 | |
wkh@mayhem ~/c/sync_with_stdio_benchmark> ./run_benchmarks.sh | |
Compiling benchmark application... | |
Running standard I/O benchmark... | |
Standard I/O: Read 1000000 numbers in 1015893 microseconds | |
real 0m1.019s | |
user 0m1.005s | |
sys 0m0.012s | |
Running optimized I/O benchmark... | |
Optimized I/O: Read 1000000 numbers in 992252 microseconds | |
real 0m0.996s | |
user 0m0.984s | |
sys 0m0.011s | |
Running C-style I/O benchmark... | |
C-style I/O: Read 1000000 numbers in 49667 microseconds | |
real 0m0.053s | |
user 0m0.048s | |
sys 0m0.004s |
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
#!/bin/bash | |
BENCHMARK_APP="./benchmark_app" | |
# Compile the benchmark application | |
echo "Compiling benchmark application..." | |
g++ -std=c++17 -O3 -march=native benchmark.cpp -o $BENCHMARK_APP | |
echo -e "\nRunning standard I/O benchmark..." | |
time $BENCHMARK_APP standard < test_data.txt | |
echo -e "\nRunning optimized I/O benchmark..." | |
time $BENCHMARK_APP optimized < test_data.txt | |
echo -e "\nRunning C-style I/O benchmark..." | |
time $BENCHMARK_APP c_style < test_data.txt |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment