- Python High Performance(Second Edition)
- Gabriele Lanaro
- 396字
- 2021-07-09 21:01:53
Writing tests and benchmarks
Now that we have a working simulator, we can start measuring our performance and tune-up our code so that the simulator can handle as many particles as possible. As a first step, we will write a test and a benchmark.
We need a test that checks whether the results produced by the simulation are correct or not. Optimizing a program commonly requires employing multiple strategies; as we rewrite our code multiple times, bugs may easily be introduced. A solid test suite ensures that the implementation is correct at every iteration so that we are free to go wild and try different things with the confidence that, if the test suite passes, the code will still work as expected.
Our test will take three particles, simulate them for 0.1 time units, and compare the results with those from a reference implementation. A good way to organize your tests is using a separate function for each different aspect (or unit) of your application. Since our current functionality is included in the evolve method, our function will be named test_evolve. The following code shows the test_evolve implementation. Note that, in this case, we compare floating point numbers up to a certain precision through the fequal function:
def test_evolve():
particles = [Particle( 0.3, 0.5, +1),
Particle( 0.0, -0.5, -1),
Particle(-0.1, -0.4, +3)]
simulator = ParticleSimulator(particles)
simulator.evolve(0.1)
p0, p1, p2 = particles
def fequal(a, b, eps=1e-5):
return abs(a - b) < eps
assert fequal(p0.x, 0.210269)
assert fequal(p0.y, 0.543863)
assert fequal(p1.x, -0.099334)
assert fequal(p1.y, -0.490034)
assert fequal(p2.x, 0.191358)
assert fequal(p2.y, -0.365227)
if __name__ == '__main__':
test_evolve()
A test ensures the correctness of our functionality but gives little information about its running time. A benchmark is a simple and representative use case that can be run to assess the running time of an application. Benchmarks are very useful to keep score of how fast our program is with each new version that we implement.
We can write a representative benchmark by instantiating a thousand Particle objects with random coordinates and angular velocity, and feed them to a ParticleSimulator class. We then let the system evolve for 0.1 time units:
from random import uniform
def benchmark():
particles = [Particle(uniform(-1.0, 1.0),
uniform(-1.0, 1.0),
uniform(-1.0, 1.0))
for i in range(1000)]
simulator = ParticleSimulator(particles)
simulator.evolve(0.1)
if __name__ == '__main__':
benchmark()
- 零基礎搭建量化投資系統:以Python為工具
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Mastering OpenCV Android Application Programming
- 深入淺出Java虛擬機:JVM原理與實戰
- OpenStack Cloud Computing Cookbook(Fourth Edition)
- Windows Phone 7.5:Building Location-aware Applications
- 從零開始學Linux編程
- Kubernetes進階實戰
- 代替VBA!用Python輕松實現Excel編程
- C語言程序設計
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- 游戲設計的底層邏輯
- Web開發新體驗
- 基于Docker的Redis入門與實戰
- Mastering Puppet(Second Edition)