- 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()
- Objective-C應用開發全程實錄
- ReSharper Essentials
- C和C++安全編碼(原書第2版)
- Getting Started with CreateJS
- Python從入門到精通(精粹版)
- 數據結構(Java語言描述)
- Scratch 3游戲與人工智能編程完全自學教程
- Learning Python Design Patterns(Second Edition)
- Swift語言實戰精講
- Python Data Structures and Algorithms
- FPGA嵌入式項目開發實戰
- CodeIgniter Web Application Blueprints
- SignalR:Real-time Application Development(Second Edition)
- Building Business Websites with Squarespace 7(Second Edition)
- Java EE項目應用開發