From 1a4289c9debb9f71f844e399124551c2d184a5bb Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Sat, 9 Aug 2025 22:07:28 +0100 Subject: [PATCH 1/3] Add microbenchmark for `sorted` I chose 5 * Iterations to try better show that RustPython sort implementation scales noticeably worse CPython's with respect to the number of elements. --- benches/microbenchmarks/sorted.py | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 benches/microbenchmarks/sorted.py diff --git a/benches/microbenchmarks/sorted.py b/benches/microbenchmarks/sorted.py new file mode 100644 index 0000000000..8e5e0beeb1 --- /dev/null +++ b/benches/microbenchmarks/sorted.py @@ -0,0 +1,9 @@ +from random import random, seed +seed(0) + +unsorted_list = [random() for _ in range(5 * ITERATIONS)] + +# --- + +# Setup code only runs once so do not modify in-place +sorted(unsorted_list) From 6edde98921bd282769bdec4416c1b459f0b657d8 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Sat, 9 Aug 2025 22:57:17 +0100 Subject: [PATCH 2/3] Mention how to run a specific benchmark --- benches/README.md | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/benches/README.md b/benches/README.md index f6e5129617..53afb72a8c 100644 --- a/benches/README.md +++ b/benches/README.md @@ -4,19 +4,26 @@ These are some files to determine performance of rustpython. ## Usage -Running `cargo bench` from the root of the repository will start the benchmarks. Once done there will be a graphical +Running `cargo bench` from the root of the repository will start the benchmarks. Once done there will be a graphical report under `target/criterion/report/index.html` that you can use use to view the results. -To view Python tracebacks during benchmarks, run `RUST_BACKTRACE=1 cargo bench`. You can also bench against a +`cargo bench` supports name matching to run a subset of the benchmarks. To +run only the sorted microbenchmark, you can run: + +```shell +cargo bench sorted +``` + +To view Python tracebacks during benchmarks, run `RUST_BACKTRACE=1 cargo bench`. You can also bench against a specific installed Python version by running: ```shell -$ PYTHON_SYS_EXECUTABLE=python3.7 cargo bench +PYTHON_SYS_EXECUTABLE=python3.7 cargo bench ``` ### Adding a benchmark -Simply adding a file to the `benchmarks/` directory will add it to the set of files benchmarked. Each file is tested +Simply adding a file to the `benchmarks/` directory will add it to the set of files benchmarked. Each file is tested in two ways: 1. The time to parse the file to AST @@ -24,8 +31,9 @@ in two ways: ### Adding a micro benchmark -Micro benchmarks are small snippets of code added under the `microbenchmarks/` directory. A microbenchmark file has +Micro benchmarks are small snippets of code added under the `microbenchmarks/` directory. A microbenchmark file has two sections: + 1. Optional setup code 2. The code to be benchmarked @@ -39,8 +47,8 @@ a_list = [1,2,3] len(a_list) ``` -Only `len(a_list)` will be timed. Setup or benchmarked code can optionally reference a variable called `ITERATIONS`. If -present then the benchmark code will be invoked 5 times with `ITERATIONS` set to a value between 100 and 1,000. For +Only `len(a_list)` will be timed. Setup or benchmarked code can optionally reference a variable called `ITERATIONS`. If +present then the benchmark code will be invoked 5 times with `ITERATIONS` set to a value between 100 and 1,000. For example: ```python @@ -49,7 +57,7 @@ obj = [i for i in range(ITERATIONS)] `ITERATIONS` can appear in both the setup code and the benchmark code. -## MacOS setup +## MacOS setup On MacOS you will need to add the following to a `.cargo/config` file: @@ -63,4 +71,4 @@ rustflags = [ ## Benchmark source -- https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-python3-2.html +- From 064a0e0ea28b222481636c0a08061478fac85527 Mon Sep 17 00:00:00 2001 From: Jack O'Connor Date: Wed, 13 Aug 2025 22:42:35 +0100 Subject: [PATCH 3/3] Update python version in bench README 3.13 better reflects the current state of the project vs 3.7. --- benches/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benches/README.md b/benches/README.md index 53afb72a8c..4823751fd1 100644 --- a/benches/README.md +++ b/benches/README.md @@ -18,7 +18,7 @@ To view Python tracebacks during benchmarks, run `RUST_BACKTRACE=1 cargo bench`. specific installed Python version by running: ```shell -PYTHON_SYS_EXECUTABLE=python3.7 cargo bench +PYTHON_SYS_EXECUTABLE=python3.13 cargo bench ``` ### Adding a benchmark