bench.cpp (813B)
1 #include <benchmark/benchmark.h> 2 3 extern "C" { 4 #include "http/request.h" 5 #include <string.h> 6 7 const char* method_names[] = {"GET", "HEAD", "POST", "PUT", "DELETE", "CONNECT", "OPTIONS", "TRACE"}; 8 9 enum http_method method_naive_enum(char* p) { 10 for (int i = 0; i < 8; i++) { 11 if (strcmp(method_names[i], p) == 0) { 12 return (enum http_method) i; 13 } 14 } 15 } 16 } 17 18 static void BM_PHF(benchmark::State& state) { 19 // Perform setup here 20 for (auto _ : state) { 21 // This code gets timed 22 method_enum((char*)"OPTIONS"); 23 } 24 } 25 26 static void BM_Naive(benchmark::State& state) { 27 // Perform setup here 28 for (auto _ : state) { 29 // This code gets timed 30 method_naive_enum((char*)"OPTIONS"); 31 } 32 } 33 34 // Register the function as a benchmark 35 BENCHMARK(BM_PHF); 36 BENCHMARK(BM_Naive); 37 // Run the benchmark 38 BENCHMARK_MAIN();