example.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. To run this example, you must first install the `google_benchmark` Python package.
  16. To install using `setup.py`, download and extract the `google_benchmark` source.
  17. In the extracted directory, execute:
  18. python setup.py install
  19. """
  20. import random
  21. import time
  22. import google_benchmark as benchmark
  23. from google_benchmark import Counter
  24. @benchmark.register
  25. def empty(state):
  26. while state:
  27. pass
  28. @benchmark.register
  29. def sum_million(state):
  30. while state:
  31. sum(range(1_000_000))
  32. @benchmark.register
  33. def pause_timing(state):
  34. """Pause timing every iteration."""
  35. while state:
  36. # Construct a list of random ints every iteration without timing it
  37. state.pause_timing()
  38. random_list = [random.randint(0, 100) for _ in range(100)]
  39. state.resume_timing()
  40. # Time the in place sorting algorithm
  41. random_list.sort()
  42. @benchmark.register
  43. def skipped(state):
  44. if True: # Test some predicate here.
  45. state.skip_with_error("some error")
  46. return # NOTE: You must explicitly return, or benchmark will continue.
  47. ... # Benchmark code would be here.
  48. @benchmark.register
  49. def manual_timing(state):
  50. while state:
  51. # Manually count Python CPU time
  52. start = time.perf_counter() # perf_counter_ns() in Python 3.7+
  53. # Something to benchmark
  54. time.sleep(0.01)
  55. end = time.perf_counter()
  56. state.set_iteration_time(end - start)
  57. @benchmark.register
  58. def custom_counters(state):
  59. """Collect cutom metric using benchmark.Counter."""
  60. num_foo = 0.0
  61. while state:
  62. # Benchmark some code here
  63. pass
  64. # Collect some custom metric named foo
  65. num_foo += 0.13
  66. # Automatic Counter from numbers.
  67. state.counters["foo"] = num_foo
  68. # Set a counter as a rate.
  69. state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate)
  70. # Set a counter as an inverse of rate.
  71. state.counters["foo_inv_rate"] = Counter(num_foo, Counter.kIsRate | Counter.kInvert)
  72. # Set a counter as a thread-average quantity.
  73. state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads)
  74. # There's also a combined flag:
  75. state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)
  76. @benchmark.register
  77. @benchmark.option.measure_process_cpu_time()
  78. @benchmark.option.use_real_time()
  79. def with_options(state):
  80. while state:
  81. sum(range(1_000_000))
  82. @benchmark.register(name="sum_million_microseconds")
  83. @benchmark.option.unit(benchmark.kMicrosecond)
  84. def with_options2(state):
  85. while state:
  86. sum(range(1_000_000))
  87. @benchmark.register
  88. @benchmark.option.arg(100)
  89. @benchmark.option.arg(1000)
  90. def passing_argument(state):
  91. while state:
  92. sum(range(state.range(0)))
  93. @benchmark.register
  94. @benchmark.option.range(8, limit=8 << 10)
  95. def using_range(state):
  96. while state:
  97. sum(range(state.range(0)))
  98. @benchmark.register
  99. @benchmark.option.range_multiplier(2)
  100. @benchmark.option.range(1 << 10, 1 << 18)
  101. @benchmark.option.complexity(benchmark.oN)
  102. def computing_complexity(state):
  103. while state:
  104. sum(range(state.range(0)))
  105. state.complexity_n = state.range(0)
  106. if __name__ == "__main__":
  107. benchmark.main()