14 """Example of Python using C++ benchmark framework.
16 To run this example, you must first install the `google_benchmark` Python package.
18 To install using `setup.py`, download and extract the `google_benchmark` source.
19 In the extracted directory, execute:
20 python setup.py install
26 import google_benchmark
as benchmark
27 from google_benchmark
import Counter
43 """Pause timing every iteration."""
47 random_list = [random.randint(0, 100)
for _
in range(100)]
56 state.skip_with_error(
"some error")
66 start = time.perf_counter()
69 end = time.perf_counter()
70 state.set_iteration_time(end - start)
75 """Collect cutom metric using benchmark.Counter."""
84 state.counters[
"foo"] = num_foo
86 state.counters[
"foo_rate"] = Counter(num_foo, Counter.kIsRate)
88 state.counters[
"foo_inv_rate"] = Counter(num_foo, Counter.kIsRate | Counter.kInvert)
90 state.counters[
"foo_avg"] = Counter(num_foo, Counter.kAvgThreads)
92 state.counters[
"foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)
96 @benchmark.option.measure_process_cpu_time()
97 @benchmark.option.use_real_time()
103 @benchmark.register(name=
"sum_million_microseconds")
104 @benchmark.option.unit(benchmark.kMicrosecond)
111 @benchmark.option.arg(100)
112 @benchmark.option.arg(1000)
119 @benchmark.option.range(8, limit=8 << 10)
126 @benchmark.option.range_multiplier(2)
127 @benchmark.option.range(1 << 10, 1 << 18)
128 @benchmark.option.complexity(benchmark.oN)
132 state.complexity_n = state.range(0)
135 if __name__ ==
"__main__":