Skip to content

Instantly share code, notes, and snippets.

@apalala
Last active November 6, 2023 01:45
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])
@cristinelpopescu
Copy link

Acer Aspire A315, AMD A6 dual-core 2.9GHz, 8GB ddr4 ram, AMD R5 GPU 2GB gddr5, SSD SATA, Zorin os 16.1 pro (Linux kernel 5.15):
1.9391337739998562 1.939522442999987 1.9427987970002505

@corneliusroemer
Copy link

corneliusroemer commented Feb 24, 2023

M1 Pro (2021)

Intel binaries through Rosetta 2

Python 3.10.8, osx-64 through Rosetta 2 (conda-forge)
1.580 1.589 1.591

Python 3.11.0, osx-64 through Rosetta 2 (conda-forge)
1.517 1.517 1.521

Python 3.11.2, osx-64 (binary from https://www.python.org/ftp/python/3.11.2/python-3.11.2-macos11.pkg)
1.302 1.386 1.299

Python 3.12.0a5, osx-64 through Rosetta 2 (binary from https://www.python.org/downloads/release/python-3120a5/)
1.326 1.295 1.270

Native binaries

Python 3.10.9, osx-arm64 (conda-forge)
0.762 0.762 0.769

Python 3.11.0, osx-arm64 (conda-forge)
0.637 0.637 0.637

Python 3.11.2, osx-arm64 (binary from https://www.python.org/ftp/python/3.11.2/python-3.11.2-macos11.pkg)
0.599 0.598 0.599

Python 3.12.0a5, osx-arm64 (binary from https://www.python.org/downloads/release/python-3120a5/)
0.602 0.602 0.602

Intel Pypy through Rosetta 2

pypy 3.9 (7.3.11), osx-64 through Rosetta 2 (conda-forge)
0.159 0.160 0.160

@apalala
Copy link
Author

apalala commented Feb 24, 2023

@corneliusroemer, Why not a Python 3.12.x test too?

Copy link

ghost commented May 18, 2023

Raspberry Pi 4B (4GB Ram): Python 3.9.2
pi17@rpi17:~ $ python3 bench.py
3.983417603000021 4.000531667000018 4.001489117999995
pi17@rpi17:~ $ pypy bench.py
0.395509004593 0.395509958267 0.395529985428

@apalala
Copy link
Author

apalala commented Jul 4, 2023

MacBook Pro 2015

$ python3.12 bench.py
1.1071506180014694 1.1081515140103875 1.1114070440089563
$ pypy3
Python 3.10.12 (af44d0b8114cb82c40a07bb9ee9c1ca8a1b3688c, Jun 25 2023, 12:41:21)
[PyPy 7.3.12 with GCC Apple LLVM 14.0.0 (clang-1400.0.29.202)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>>

$ pypy3 -O bench.py
0.1369127530051628 0.13725044101011008 0.13727515301434323

@apalala
Copy link
Author

apalala commented Jul 20, 2023

Macbook Air M2 2023

$ python3.11 -O bench.py
0.47316745800344506 0.47342624999873806 0.4737870000026305

$ python3.12 -O bench.py
0.5029823330041836 0.5032062500031316 0.5033741669976735

$ pypy3 -O bench.py
0.03534275000129128 0.03537720799795352 0.0354161660070531

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