Skip to content

Instantly share code, notes, and snippets.

@patricklucas
Last active December 14, 2015 05:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save patricklucas/5033979 to your computer and use it in GitHub Desktop.
Save patricklucas/5033979 to your computer and use it in GitHub Desktop.
Demonstration of CPython vs PyPy performance on a simple task. From the docstring: "This demonstrates that the expectation of the number of random values on the interval 0 <= r < 1 which sum to greater than 1 is the constant 'e'."
from __future__ import division
import random
def experiment(num_trials):
"""This demonstrates that the expectation of the number of random values
on the interval 0 <= r < 1 which sum to greater than 1 is the constant 'e'.
"""
total_num_values = 0
for _ in xrange(num_trials):
value_total = 0.0
num_values = 0
while value_total <= 1:
value_total += random.random()
num_values += 1
total_num_values += num_values
return total_num_values / num_trials
print experiment(10000000)
System:
Mac OS X 10.7.5
Intel Core i7 2.2 GHz
8 GB 1333 MHz DDR 3
### Python 2.7.1
$ time python pypy_experiment.py
2.7182563
real 0m11.451s
user 0m11.434s
sys 0m0.015s
$ time python pypy_experiment.py
2.7180425
real 0m11.321s
user 0m11.300s
sys 0m0.019s
$ time python pypy_experiment.py
2.7183524
real 0m10.844s
user 0m10.829s
sys 0m0.013s
### PyPy 1.9.0 (Python 2.7.2)
$ time pypy pypy_experiment.py
2.7182537
real 0m1.415s
user 0m1.381s
sys 0m0.016s
$ time pypy pypy_experiment.py
2.7187915
real 0m1.396s
user 0m1.379s
sys 0m0.015s
$ time pypy pypy_experiment.py
2.7184903
real 0m1.392s
user 0m1.377s
sys 0m0.014s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment