1# Copyright 2020 Google Inc. All rights reserved. 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Example of Python using C++ benchmark framework. 15 16To run this example, you must first install the `google_benchmark` Python package. 17 18To install using `setup.py`, download and extract the `google_benchmark` source. 19In the extracted directory, execute: 20 python setup.py install 21""" 22 23import random 24import time 25 26import google_benchmark as benchmark 27from google_benchmark import Counter 28 29 30@benchmark.register 31def empty(state): 32 while state: 33 pass 34 35 36@benchmark.register 37def sum_million(state): 38 while state: 39 sum(range(1_000_000)) 40 41@benchmark.register 42def pause_timing(state): 43 """Pause timing every iteration.""" 44 while state: 45 # Construct a list of random ints every iteration without timing it 46 state.pause_timing() 47 random_list = [random.randint(0, 100) for _ in range(100)] 48 state.resume_timing() 49 # Time the in place sorting algorithm 50 random_list.sort() 51 52 53@benchmark.register 54def skipped(state): 55 if True: # Test some predicate here. 56 state.skip_with_error("some error") 57 return # NOTE: You must explicitly return, or benchmark will continue. 58 59 ... # Benchmark code would be here. 60 61 62@benchmark.register 63def manual_timing(state): 64 while state: 65 # Manually count Python CPU time 66 start = time.perf_counter() # perf_counter_ns() in Python 3.7+ 67 # Something to benchmark 68 time.sleep(0.01) 69 end = time.perf_counter() 70 state.set_iteration_time(end - start) 71 72 73@benchmark.register 74def custom_counters(state): 75 """Collect cutom metric using benchmark.Counter.""" 76 num_foo = 0.0 77 while state: 78 # Benchmark some code here 79 pass 80 # Collect some custom metric named foo 81 num_foo += 0.13 82 83 # Automatic Counter from numbers. 84 state.counters["foo"] = num_foo 85 # Set a counter as a rate. 86 state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate) 87 # Set a counter as an inverse of rate. 88 state.counters["foo_inv_rate"] = Counter(num_foo, Counter.kIsRate | Counter.kInvert) 89 # Set a counter as a thread-average quantity. 90 state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads) 91 # There's also a combined flag: 92 state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate) 93 94 95@benchmark.register 96@benchmark.option.measure_process_cpu_time() 97@benchmark.option.use_real_time() 98def with_options(state): 99 while state: 100 sum(range(1_000_000)) 101 102 103@benchmark.register(name="sum_million_microseconds") 104@benchmark.option.unit(benchmark.kMicrosecond) 105def with_options2(state): 106 while state: 107 sum(range(1_000_000)) 108 109 110@benchmark.register 111@benchmark.option.arg(100) 112@benchmark.option.arg(1000) 113def passing_argument(state): 114 while state: 115 sum(range(state.range(0))) 116 117 118@benchmark.register 119@benchmark.option.range(8, limit=8 << 10) 120def using_range(state): 121 while state: 122 sum(range(state.range(0))) 123 124 125@benchmark.register 126@benchmark.option.range_multiplier(2) 127@benchmark.option.range(1 << 10, 1 << 18) 128@benchmark.option.complexity(benchmark.oN) 129def computing_complexity(state): 130 while state: 131 sum(range(state.range(0))) 132 state.complexity_n = state.range(0) 133 134 135if __name__ == "__main__": 136 benchmark.main() 137