File: stoi.cpp

package info (click to toggle)
gemmi 0.5.7%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,344 kB
  • sloc: cpp: 48,972; python: 4,352; ansic: 3,428; sh: 302; makefile: 69; f90: 42; javascript: 12
file content (35 lines) | stat: -rw-r--r-- 974 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include <cstdio>
#include <string>
#include <vector>
#include <benchmark/benchmark.h>
#include <gemmi/atox.hpp>

static int std_stoi(const std::string& str) {
  return std::stoi(str);
}
static int to_int_true(const std::string& str) {
  return gemmi::string_to_int(str, true);
}

static int to_int_false(const std::string& str) {
  return gemmi::string_to_int(str, false);
}

void sequential(benchmark::State& state, int(*func)(const std::string&)) {
  std::vector<std::string> v(20000);
  for (size_t i = 0; i != v.size(); ++i)
    v[i] = std::to_string(i);
  for (size_t i = 0; i != v.size(); ++i)
    if ((*func)(v[i]) != (int)i)
      std::printf("ERROR: at %zu\n", i);
  while (state.KeepRunning())
    for (const std::string& s : v)
      benchmark::DoNotOptimize((*func)(s));
}


BENCHMARK_CAPTURE(sequential, std_stoi, std_stoi);
BENCHMARK_CAPTURE(sequential, to_int_false, to_int_false);
BENCHMARK_CAPTURE(sequential, to_int_true, to_int_true);
BENCHMARK_MAIN();