Skip to content

Instantly share code, notes, and snippets.

@howardlau1999
Created August 13, 2021 15:12
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 howardlau1999/eda64958ec8716cace7310288dc80b46 to your computer and use it in GitHub Desktop.
Save howardlau1999/eda64958ec8716cace7310288dc80b46 to your computer and use it in GitHub Desktop.
benchmark reading lots of float numbers
#include <benchmark/benchmark.h>
#include <charconv>
#include <fcntl.h>
#include <fstream>
#include <iostream>
#include <cstdlib>
#include <sys/file.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
const char *filename = "n.dat";
float dat0, dat1, pri0, pri1, ctf0, sigRcp0;
static void FSTREAM(benchmark::State &state) {
std::ios::sync_with_stdio(false);
for (auto _ : state) {
std::ifstream fin;
fin.open(filename);
int counter = 0;
while (!fin.eof()) {
fin >> dat0 >> dat1 >> pri0 >> pri1 >> ctf0 >> sigRcp0;
++counter;
benchmark::ClobberMemory();
}
}
}
BENCHMARK(FSTREAM)->Unit(benchmark::kMillisecond);
static void FSCANF(benchmark::State &state) {
std::ios::sync_with_stdio(false);
for (auto _ : state) {
FILE *fp = fopen(filename, "r");
int counter = 0;
while (~fscanf(fp, "%f%f%f%f%f%f", &dat0, &dat1, &pri0, &pri1, &ctf0,
&sigRcp0)) {
counter++;
benchmark::ClobberMemory();
}
fclose(fp);
}
}
BENCHMARK(FSCANF)->Unit(benchmark::kMillisecond);
inline float ParseOneFloatStrtof(char *&start, const char *end) {
char* p;
float val = strtof(start, &p);
start = p == end ? p : (p + 1);
return val;
}
inline float ParseOneFloat(char *&p, const char *end) {
int integral = 0;
while (*p != '.') {
integral *= 10;
integral += (*p) - '0';
p++;
}
p++;
int mantissa = 0;
float base = 1.0f;
while (p != end && *p != '\t' && *p != '\n') {
mantissa *= 10;
mantissa += (*p) - '0';
base *= 0.1f;
p++;
}
if (p != end) p++;
return mantissa * base + integral;
}
static void MMAP(benchmark::State &state) {
for (auto _ : state) {
int fd = open(filename, O_RDONLY, 0666);
off_t fileLength = lseek(fd, 0, SEEK_END);
char *start = static_cast<char *>(
mmap(NULL, fileLength, PROT_READ, MAP_PRIVATE, fd, 0));
char *end = start + fileLength;
char *p = start;
int counter = 0;
while (p != end) {
dat0 = ParseOneFloat(p, end);
dat1 = ParseOneFloat(p, end);
pri0 = ParseOneFloat(p, end);
pri1 = ParseOneFloat(p, end);
ctf0 = ParseOneFloat(p, end);
sigRcp0 = ParseOneFloat(p, end);
counter++;
benchmark::ClobberMemory();
}
munmap(start, fileLength);
close(fd);
}
}
BENCHMARK(MMAP)->Unit(benchmark::kMillisecond);
BENCHMARK_MAIN();
N = 1638400
import random
for _ in range(N):
print('\t'.join([f"{random.random() * 10 + 40:.2f}" for _ in range(6)]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment