Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / bindings / python / google_benchmark / example.py
blobfb0234b8fd7e31d01f7594fd05a14a06b45b81db
1 # Copyright 2020 Google Inc. All rights reserved.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
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
21 """
23 import random
24 import time
26 import google_benchmark as benchmark
27 from google_benchmark import Counter
30 @benchmark.register
31 def empty(state):
32 while state:
33 pass
36 @benchmark.register
37 def sum_million(state):
38 while state:
39 sum(range(1_000_000))
42 @benchmark.register
43 def pause_timing(state):
44 """Pause timing every iteration."""
45 while state:
46 # Construct a list of random ints every iteration without timing it
47 state.pause_timing()
48 random_list = [random.randint(0, 100) for _ in range(100)]
49 state.resume_timing()
50 # Time the in place sorting algorithm
51 random_list.sort()
54 @benchmark.register
55 def skipped(state):
56 if True: # Test some predicate here.
57 state.skip_with_error("some error")
58 return # NOTE: You must explicitly return, or benchmark will continue.
60 ... # Benchmark code would be here.
63 @benchmark.register
64 def manual_timing(state):
65 while state:
66 # Manually count Python CPU time
67 start = time.perf_counter() # perf_counter_ns() in Python 3.7+
68 # Something to benchmark
69 time.sleep(0.01)
70 end = time.perf_counter()
71 state.set_iteration_time(end - start)
74 @benchmark.register
75 def custom_counters(state):
76 """Collect cutom metric using benchmark.Counter."""
77 num_foo = 0.0
78 while state:
79 # Benchmark some code here
80 pass
81 # Collect some custom metric named foo
82 num_foo += 0.13
84 # Automatic Counter from numbers.
85 state.counters["foo"] = num_foo
86 # Set a counter as a rate.
87 state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate)
88 # Set a counter as an inverse of rate.
89 state.counters["foo_inv_rate"] = Counter(num_foo, Counter.kIsRate | Counter.kInvert)
90 # Set a counter as a thread-average quantity.
91 state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads)
92 # There's also a combined flag:
93 state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)
96 @benchmark.register
97 @benchmark.option.measure_process_cpu_time()
98 @benchmark.option.use_real_time()
99 def with_options(state):
100 while state:
101 sum(range(1_000_000))
104 @benchmark.register(name="sum_million_microseconds")
105 @benchmark.option.unit(benchmark.kMicrosecond)
106 def with_options2(state):
107 while state:
108 sum(range(1_000_000))
111 @benchmark.register
112 @benchmark.option.arg(100)
113 @benchmark.option.arg(1000)
114 def passing_argument(state):
115 while state:
116 sum(range(state.range(0)))
119 @benchmark.register
120 @benchmark.option.range(8, limit=8 << 10)
121 def using_range(state):
122 while state:
123 sum(range(state.range(0)))
126 @benchmark.register
127 @benchmark.option.range_multiplier(2)
128 @benchmark.option.range(1 << 10, 1 << 18)
129 @benchmark.option.complexity(benchmark.oN)
130 def computing_complexity(state):
131 while state:
132 sum(range(state.range(0)))
133 state.complexity_n = state.range(0)
136 if __name__ == "__main__":
137 benchmark.main()