brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.8 KiB · b5b2f88 Raw
140 lines · python
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 at6#7#     http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# 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 and13# 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 install21"""22 23import random24import time25 26import google_benchmark as benchmark27from google_benchmark import Counter28 29 30@benchmark.register31def empty(state):32    while state:33        pass34 35 36@benchmark.register37def sum_million(state):38    while state:39        sum(range(1_000_000))40 41 42@benchmark.register43def pause_timing(state):44    """Pause timing every iteration."""45    while state:46        # Construct a list of random ints every iteration without timing it47        state.pause_timing()48        random_list = [random.randint(0, 100) for _ in range(100)]49        state.resume_timing()50        # Time the in place sorting algorithm51        random_list.sort()52 53 54@benchmark.register55def 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.59 60    ...  # Benchmark code would be here.61 62 63@benchmark.register64def manual_timing(state):65    while state:66        # Manually count Python CPU time67        start = time.perf_counter()  # perf_counter_ns() in Python 3.7+68        # Something to benchmark69        time.sleep(0.01)70        end = time.perf_counter()71        state.set_iteration_time(end - start)72 73 74@benchmark.register75def custom_counters(state):76    """Collect custom metric using benchmark.Counter."""77    num_foo = 0.078    while state:79        # Benchmark some code here80        pass81        # Collect some custom metric named foo82        num_foo += 0.1383 84    # Automatic Counter from numbers.85    state.counters["foo"] = num_foo86    # 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(90        num_foo, Counter.kIsRate | Counter.kInvert91    )92    # Set a counter as a thread-average quantity.93    state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads)94    # There's also a combined flag:95    state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)96 97 98@benchmark.register99@benchmark.option.measure_process_cpu_time()100@benchmark.option.use_real_time()101def with_options(state):102    while state:103        sum(range(1_000_000))104 105 106@benchmark.register(name="sum_million_microseconds")107@benchmark.option.unit(benchmark.kMicrosecond)108def with_options2(state):109    while state:110        sum(range(1_000_000))111 112 113@benchmark.register114@benchmark.option.arg(100)115@benchmark.option.arg(1000)116def passing_argument(state):117    while state:118        sum(range(state.range(0)))119 120 121@benchmark.register122@benchmark.option.range(8, limit=8 << 10)123def using_range(state):124    while state:125        sum(range(state.range(0)))126 127 128@benchmark.register129@benchmark.option.range_multiplier(2)130@benchmark.option.range(1 << 10, 1 << 18)131@benchmark.option.complexity(benchmark.oN)132def computing_complexity(state):133    while state:134        sum(range(state.range(0)))135    state.complexity_n = state.range(0)136 137 138if __name__ == "__main__":139    benchmark.main()140