cb66c5d68e
bazel / bazel.ubuntu-latest (push) Failing after 1s
build-and-test-perfcounters / ubuntu-latest.Debug (push) Failing after 0s
build-and-test-perfcounters / ubuntu-latest.Release (push) Failing after 1s
doxygen / Build HTML documentation (push) Failing after 1s
python + Bazel pre-commit checks / pre-commit (push) Failing after 1s
sanitizer / tsan.Debug (push) Failing after 1s
sanitizer / ubsan.Debug (push) Failing after 0s
sanitizer / ubsan.RelWithDebInfo (push) Failing after 1s
build-and-test-min-cmake / ubuntu-latest.min-cmake (push) Failing after 1s
clang-format-lint / check-clang-format (push) Failing after 1s
sanitizer / asan.Debug (push) Failing after 1s
sanitizer / msan.RelWithDebInfo (push) Failing after 1s
clang-tidy / run-clang-tidy (push) Failing after 1s
sanitizer / msan.Debug (push) Failing after 1s
sanitizer / asan.RelWithDebInfo (push) Failing after 0s
sanitizer / tsan.RelWithDebInfo (push) Failing after 0s
test-bindings / Test Rust bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.10 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.11 bindings on ubuntu-latest (push) Failing after 0s
test-bindings / Test Python 3.12 bindings on ubuntu-latest (push) Failing after 1s
test-bindings / Test Python 3.13 bindings on ubuntu-latest (push) Failing after 0s
build-and-test / macos-latest.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.shared.clang++ (push) Has been cancelled
bazel / bazel.macos-latest (push) Has been cancelled
bazel / bazel.windows-latest (push) Has been cancelled
build-and-test-min-cmake / macos-latest.min-cmake (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.shared.g++ (push) Has been cancelled
build-and-test / macos-latest.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.static.g++ (push) Has been cancelled
build-and-test / macos-latest.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.shared.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.shared.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Debug.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Debug.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Debug.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.static.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.static.g++ (push) Has been cancelled
build-and-test / windows-2025.Debug.static.VS-18-2025 (push) Has been cancelled
build-and-test / windows-2022.Release.shared.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2022.Release.static.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2025.Release.shared.VS-18-2025 (push) Has been cancelled
build-and-test / windows-2025.Release.static.VS-18-2025 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Debug.shared.UCRT64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Debug.static.UCRT64 (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.static.clang++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.static.clang++ (push) Has been cancelled
build-and-test / macos-latest.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-22.04.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.shared.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04.Release.static.g++ (push) Has been cancelled
build-and-test / ubuntu-24.04-arm.Release.static.g++ (push) Has been cancelled
build-and-test / windows-2022.Debug.shared.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2022.Debug.static.VS-17-2022 (push) Has been cancelled
build-and-test / windows-2025.Debug.shared.VS-18-2025 (push) Has been cancelled
build-and-test / windows-latest.Release.static.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Release.static.UCRT64 (push) Has been cancelled
test-bindings / Test Python 3.10 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.11 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.12 bindings on macos-latest (push) Has been cancelled
test-bindings / Test Python 3.13 bindings on macos-latest (push) Has been cancelled
build-and-test / windows-latest.Release.shared.CLANG64 (push) Has been cancelled
build-and-test / windows-latest.Release.shared.MINGW64 (push) Has been cancelled
build-and-test / windows-latest.Release.shared.UCRT64 (push) Has been cancelled
build-and-test / windows-latest.Release.static.CLANG64 (push) Has been cancelled
test-bindings / Test Python 3.12 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.13 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.10 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Python 3.11 bindings on windows-latest (push) Has been cancelled
test-bindings / Test Rust bindings on macos-latest (push) Has been cancelled
test-bindings / Test Rust bindings on windows-latest (push) Has been cancelled
143 lines
3.9 KiB
Python
143 lines
3.9 KiB
Python
# Copyright 2020 Google Inc. All rights reserved.
|
|
#
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
"""Example of Python using C++ benchmark framework.
|
|
|
|
To run this example, you must first install the `google_benchmark` Python
|
|
package.
|
|
|
|
To install using `setup.py`, download and extract the `google_benchmark` source.
|
|
In the extracted directory, execute:
|
|
python setup.py install
|
|
"""
|
|
|
|
import random
|
|
import sys
|
|
import time
|
|
|
|
import google_benchmark as benchmark
|
|
from google_benchmark import Counter
|
|
|
|
|
|
@benchmark.register
|
|
def empty(state):
|
|
while state:
|
|
pass
|
|
|
|
|
|
@benchmark.register
|
|
def sum_million(state):
|
|
while state:
|
|
sum(range(1_000_000))
|
|
|
|
|
|
@benchmark.register
|
|
def pause_timing(state):
|
|
"""Pause timing every iteration."""
|
|
while state:
|
|
# Construct a list of random ints every iteration without timing it
|
|
state.pause_timing()
|
|
random_list = [random.randint(0, 100) for _ in range(100)]
|
|
state.resume_timing()
|
|
# Time the in place sorting algorithm
|
|
random_list.sort()
|
|
|
|
|
|
@benchmark.register
|
|
def skipped(state):
|
|
if True: # Test some predicate here.
|
|
state.skip_with_error("some error")
|
|
return # NOTE: You must explicitly return, or benchmark will continue.
|
|
|
|
# Benchmark code would be here.
|
|
|
|
|
|
@benchmark.register
|
|
@benchmark.option.use_manual_time()
|
|
def manual_timing(state):
|
|
while state:
|
|
# Manually count Python CPU time
|
|
start = time.perf_counter() # perf_counter_ns() in Python 3.7+
|
|
# Something to benchmark
|
|
time.sleep(0.01)
|
|
end = time.perf_counter()
|
|
state.set_iteration_time(end - start)
|
|
|
|
|
|
@benchmark.register
|
|
def custom_counters(state):
|
|
"""Collect custom metric using benchmark.Counter."""
|
|
num_foo = 0.0
|
|
while state:
|
|
# Benchmark some code here
|
|
# Collect some custom metric named foo
|
|
num_foo += 0.13
|
|
|
|
# Automatic Counter from numbers.
|
|
state.counters["foo"] = num_foo
|
|
# Set a counter as a rate.
|
|
state.counters["foo_rate"] = Counter(num_foo, Counter.kIsRate)
|
|
# Set a counter as an inverse of rate.
|
|
state.counters["foo_inv_rate"] = Counter(
|
|
num_foo, Counter.kIsRate | Counter.kInvert
|
|
)
|
|
# Set a counter as a thread-average quantity.
|
|
state.counters["foo_avg"] = Counter(num_foo, Counter.kAvgThreads)
|
|
# There's also a combined flag:
|
|
state.counters["foo_avg_rate"] = Counter(num_foo, Counter.kAvgThreadsRate)
|
|
|
|
|
|
@benchmark.register
|
|
@benchmark.option.measure_process_cpu_time()
|
|
@benchmark.option.use_real_time()
|
|
def with_options(state):
|
|
while state:
|
|
sum(range(1_000_000))
|
|
|
|
|
|
@benchmark.register(name="sum_million_microseconds")
|
|
@benchmark.option.unit(benchmark.kMicrosecond)
|
|
def with_options2(state):
|
|
while state:
|
|
sum(range(1_000_000))
|
|
|
|
|
|
@benchmark.register
|
|
@benchmark.option.arg(100)
|
|
@benchmark.option.arg(1000)
|
|
def passing_argument(state):
|
|
while state:
|
|
sum(range(state.range(0)))
|
|
|
|
|
|
@benchmark.register
|
|
@benchmark.option.range(8, limit=8 << 10)
|
|
def using_range(state):
|
|
while state:
|
|
sum(range(state.range(0)))
|
|
|
|
|
|
@benchmark.register
|
|
@benchmark.option.range_multiplier(2)
|
|
@benchmark.option.range(1 << 10, 1 << 18)
|
|
@benchmark.option.complexity(benchmark.oN)
|
|
def computing_complexity(state):
|
|
while state:
|
|
sum(range(state.range(0)))
|
|
state.complexity_n = state.range(0)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
benchmark.add_custom_context("python", sys.version)
|
|
benchmark.main()
|