Running a particle simulator in PyPy

Now that we have successfully set up the PyPy installation, we can go ahead and run our particle simulator. As a first step, we will time the particle simulator from Chapter 1Benchmarking and Profiling, on the standard Python interpreter. If the virtual environment is still active, you can issue the command deactivate to exit the environment. We can confirm that the Python interpreter is the standard one by using the python -V command:

(my-pypy-env) $ deactivate
$ python -V
Python 3.5.2 :: Continuum Analytics, Inc.

At this point, we can time our code using the timeit command-line interface:

$ python -m timeit --setup "from simul import benchmark" "benchmark()"
10 loops, best of 3: 886 msec per loop

We can reactivate the environment and run the exact same code from PyPy. On Ubuntu, you may have problems importing the matplotlib.pyplot module. You can try issuing the following export command to fix the issue or removing the matplotlib imports from simul.py:

$ export MPLBACKEND='agg'

Now, we can go ahead and time the code using PyPy:

$ source my-pypy-env/bin/activate
Python 2.7.12 (aff251e54385, Nov 09 2016, 18:02:49)
[PyPy 5.6.0 with GCC 4.8.2]

(my-pypy-env) $ python -m timeit --setup "from simul import benchmark" "benchmark()"
WARNING: timeit is a very unreliable tool. use perf or something else for real measurements
10 loops, average of 7: 106 +- 0.383 msec per loop (using standard deviation)

Note that we obtained a large, more than eight times, speedup! PyPy, however, warns us that the timeit module can be unreliable. We can confirm our timings using the perf module, as suggested by PyPy:

(my-pypy-env) $ pip install perf
(my-pypy-env) $ python -m perf timeit --setup 'from simul import benchmark' 'benchmark()'

.......
Median +- std dev: 97.8 ms +- 2.3 ms
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.162.37