I want to compare this simple C code:
float f(float x[], float y[]) { float p = 0; for (int i = 0; i <64; i++) p += x[i] * y[i]; return p; }
My motivation is to try different compiler flags, as well as gcc and clang, to find out what the difference is.
I found this test structure and tried to make it work. Although I am completely new to C ++, here are my best efforts:
#include <benchmark.h> #include <benchmark_api.h> #include <cstdio> #include <random> std::random_device seed; std::mt19937 gen(seed()); float f(float* x, float* y) { float p = 0; for (int i = 0; i <64; i++) { p += x[i] * y[i]; } return p; } void f_benchmark(benchmark::State& state) { while (state.KeepRunning()) { benchmark::DoNotOptimize(f((float*) state.range(0), (float*) state.range(1))); } } void args(benchmark::internal::Benchmark* b) { std::uniform_real_distribution<float> rand(0, 100); for (int i = 0; i < 10; i++) { float* x = new float[64]; float* y = new float[64]; for (int i = 0; i < 64; i++) { x[i] = rand(gen); y[i] = rand(gen); printf("%f %f\n", x[i], y[i]); } b->Args({(int) x, (int) y}); } } BENCHMARK(f_benchmark)->Apply(args); BENCHMARK_MAIN();
To compile it, follow these steps:
g ++ -Ofast -Wall -std = C ++ 11 test.cpp -Ibenchmark / include / benchmark / -Lbenchmark / src / -o test -lbenchmark -lpthread
This gives me:
test.cpp: In function 'void f_benchmark(benchmark::State&)': test.cpp:20:54: warning: cast to pointer from integer of different size [-Wint-to-pointer-cast] benchmark::DoNotOptimize(f((float*) state.range(0), (float*) state.range(1))); [...] test.cpp: In function 'void args(benchmark::internal::Benchmark*)': test.cpp:38:20: error: cast from 'float*' to 'int' loses precision [-fpermissive] b->Args({(int) x, (int) y}); ^ [...]
How can I get rid of these warnings and in general, am I doing this right?
source share