Last active
December 14, 2015 05:18
-
-
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'."
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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