Skip to content

Instantly share code, notes, and snippets.

@apalala
Last active December 8, 2024 16:27
Show Gist options
  • Save apalala/3fbbeb5305584d2abe05 to your computer and use it in GitHub Desktop.
Save apalala/3fbbeb5305584d2abe05 to your computer and use it in GitHub Desktop.
A simple Python benchmark
from __future__ import print_function
from math import sin, cos, radians
import timeit
'''
A simple Python benchmark.
Results on an overclocked AMD FX-8150 Eight-Core CPU @ 3.0 GHz, and
an Intel Core i5-2410M CPU @ 2.30GHz.
$ python -OO bench.py
1.99843406677 2.00139904022 2.0145778656
2.38226699829 2.38675498962 2.38853287697
$ python3 -OO bench.py
2.2073315899979207 2.2098999509980786 2.222747125000751
2.273064840992447 2.274112678001984 2.2759074380010134
$ pypy -OO bench.py
0.245079994202 0.24707698822 0.247714996338
0.241708040237 0.242873907089 0.245008945465
$ pypy3 -OO bench.py
1.1291401386260986 1.1360960006713867 1.1375579833984375
1.2108190059661865 1.2172389030456543 1.2178328037261963
'''
def bench():
product = 1.0
for counter in range(1, 1000, 1):
for dex in list(range(1, 360, 1)):
angle = radians(dex)
product *= sin(angle)**2 + cos(angle)**2
return product
if __name__ == '__main__':
result = timeit.repeat('bench.bench()', setup='import bench', number=10, repeat=10)
result = list(sorted(result))
print(*result[:3])
@Tarliton
Copy link

Tarliton commented Oct 9, 2024

Raspberry Pi 5 Model B Rev 1.0

Official python docker image 3.13.0:

$ python
Python 3.13.0 (main, Oct  8 2024, 00:55:30) [GCC 13.2.1 20240309] on linux

$ docker run --rm -it -v $(pwd):/bench python:3.13-alpine python /bench/bench.py
1.379287590039894 1.3798810039879754 1.3804634480038658

Custom python 3.13.0 compiled with --enable-experimental-jit:

$ python
Python 3.13.0 (main, Oct  9 2024, 01:04:00) [GCC 14.2.0] on linux

$ docker run --rm -it -v $(pwd):/bench my_custom_python /root/.pyenv/versions/3.13.0/bin/python /bench/bench.py
1.1263249380281195 1.1263620959362015 1.1266266249585897

Not bad.

@apalala
Copy link
Author

apalala commented Dec 8, 2024

MacBook Air M2 2023

$ python3.13 --version
Python 3.13.1

$ python3.13 bench.py
0.5406068330048583 0.5409433339955285 0.5412480410013814

$ python3.13 -OO bench.py
0.5364589579985477 0.5369448330020532 0.5370732499868609

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment