项目作者: boisgera

项目描述 :
Doctests Benchmarking
高级语言: Python
项目地址: git://github.com/boisgera/docbench.git
创建时间: 2013-03-30T11:04:24Z
项目社区:https://github.com/boisgera/docbench

开源协议:

下载


Build status

Docbench: Doctests Benchmarking

Docbench is a framework based on doctest to benchmark Python code.
If you already are familiar with doctest,
you should be able to use docbench in minutes.

Example

We will walk you through the use of docbench with a simple use case:
a module that computes prime numbers.

Create a primes.py file and define the primes function:

  1. def primes(n):
  2. p = []
  3. for i in range(2, n+1):
  4. for j in range(2, i):
  5. if (i % j == 0):
  6. break
  7. else:
  8. p.append(i)
  9. return p

The function returns an ordered list of primes numbers up to the number n.
Its implementation is simple but the execution takes a lot of time when n grows.
The following sieve function should return the same result, but performs less
computations and therefore should be faster.

  1. def sieve(n):
  2. p = []
  3. for i in range(2, n+1):
  4. prime = True
  5. for j in p:
  6. if j * j > i:
  7. break
  8. if (i % j == 0):
  9. prime = False
  10. break
  11. if prime:
  12. p.append(i)
  13. return p

Does it Work?

If you already know doctest, you may skip this section.

Testing this module with doctest is pretty straightforward: create a test.py
file with a function test_primes with no implementation but a doctest
for the primes function:

  1. def test_primes():
  2. """
  3. >>> from primes import primes
  4. >>> n = 50
  5. >>> primes(n)
  6. [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
  7. """

You are pretty confident that if test_primes doctest works as advertised
in this doctest, the primes function behaves correctly.
Therefore, you may now test sieve against primes:

  1. def test_sieve():
  2. """
  3. >>> from primes import primes, sieve
  4. >>> n = 10000
  5. >>> primes(n) == sieve(n)
  6. True
  7. """

Finally, add the following boilerplate a the end of your file:

  1. if __name__ == "__main__":
  2. import doctest
  3. doctest.testmod()

You are ready to execute this test suite with:

  1. $ python test.py

No error displayed ? The primes module has successfully passed all tests !

Is is Fast?

Create a new file name benchmark.py. Add functions that act as docbench
holders for the function primes and sieve. We rely on the convention
that only the time spent in the last statement of any docbench will be
measured, the previous ones are considered setup code.

  1. def benchmark_primes():
  2. """
  3. >>> from primes import primes
  4. >>> n = 10000
  5. >>> primes(n)
  6. """
  7. def benchmark_sieve():
  8. """
  9. >>> from primes import sieve
  10. >>> n = 10000
  11. >>> sieve(n)
  12. """

Add the following boilerplate at the end of your file:

  1. if __name__ == "__main__":
  2. import docbench
  3. docbench.benchmod()

Run your benchmark with:

  1. $ python benchmark.py

You should end up with an output similar too:

  1. Benchmark Time
  2. ------------------------- ----------------
  3. __main__.benchmark_primes 1.03
  4. __main__.benchmark_sieve 0.00876

Indeed, sieve is quite faster than primes !