This release is a pre-release and may not be stable for production use.
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. And if you find that doctest is convenient – or Pythonic – hopefully you will feel the same about docbench !
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:
def primes(n):
p = []
for i in range(2, n+1):
for j in range(2, i):
if (i % j == 0):
break
else:
p.append(i)
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.
def sieve(n):
p = []
for i in range(2, n+1):
prime = True
for j in p:
if j * j > i:
break
if (i % j == 0):
prime = False
break
if prime:
p.append(i)
return p
Doctest – 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:
def test_primes():
"""
>>> from primes import primes
>>> n = 50
>>> primes(n)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
"""
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:
def test_sieve():
"""
>>> from primes import primes, sieve
>>> n = 10000
>>> primes(n) == sieve(n)
True
"""
Finally, add the following boilerplate a the end of your file:
if __name__ == "__main__":
import doctest
doctest.testmod()
You are ready to execute this test suite with:
$ python test.py
No error displayed ? The primes module has successfully passed all tests !
Docbench – 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.
def benchmark_primes():
"""
>>> from primes import primes
>>> n = 10000
>>> primes(n)
"""
def benchmark_sieve():
"""
>>> from primes import sieve
>>> n = 10000
>>> sieve(n)
"""
Add the following boilerplate at the end of your file:
if __name__ == "__main__":
import docbench
docbench.benchmod()
Run your benchmark with:
$ python benchmark.py
You should end up with an output similar too:
Benchmark Time ------------------------- ---------------- __main__.benchmark_primes 1.03 __main__.benchmark_sieve 0.00876
Indeed, sieve is quite faster than primes !
Metadata
Release files for docbench 1.0.0-alpha.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| docbench-1.0.0-alpha.6.tar.gz | 37.4 kB | Without provenance | Details for docbench-1.0.0-alpha.6.tar.gz |
Release files / docbench-1.0.0-alpha.6.tar.gz
| Download URL | docbench-1.0.0-alpha.6.tar.gz |
|---|---|
| Size | 37.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
9e8b231a0c283fb10043ef5ed0311a299e58b4dc7c243f97f6ca10c679618217
|
|
BLAKE2b-256 checksum How to use checksums |
b50f856ba25db6279b436b6f44abacd81fd693063b43d2b876280c81af0b305b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Security for docbench 1.0.0-alpha.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.