A method based on timeit that can help you to call timeit.timeit for several statements and provide comparison results.
Project description
timeit_compare
A method based on timeit that can help you to call timeit.timeit for several statements and provide comparison results.
Installation
You can run the following command to install the package
pip install timeit_compare
Usage
When using the timeit library, I am always more interested in comparing the efficiency of several different methods to solve a problem, rather than simply measuring the running time of a single statement. Here is a simple example.
from functools import reduce
from operator import add
n = 100
def sum1():
s = 0
i = 1
while i <= n:
s += i
i += 1
return s
def sum2():
s = 0
for i in range(1, n + 1):
s += i
return s
def sum3():
return sum(range(1, n + 1))
def sum4():
return reduce(add, range(1, n + 1))
def sum5():
return (1 + n) * n // 2
The functions above are all used to sum numbers from 1 to 100, which one is the
most efficient?
By using:
from timeit_compare import compare
compare(sum1, sum2, sum3, sum4, sum5)
you can easily get the results like:
The output provides detailed results, including the mean, median, minimum, maximum and standard deviation of each function's running time.
Release Notes
Release 1.1.0
-
The results now show the time of one loop in seconds(s), milliseconds(ms), microseconds(μs), or nanoseconds(ns) instead of the total time of each repetition. The conversion relationship among time units is as follows:
1(s) = 10^3(ms) = 10^6(μs) = 10^9(ns)
-
Now the compare function supports setting parameters setup and globals separately for each statement. Keyword parameters setup and globals are now used to set the default parameter values for each statement.
from timeit_compare import compare stmt = '(1 + n) * n // 2' compare( stmt, (stmt, 'n = 100', {}), (stmt, '', {'n': 100}), setup='n = 100' )
-
If parameter number is not given or is less than or equal to 0, it now defaults to a value that makes the total running time not too long. You can intentionally set it to a higher value to make the results more accurate.
-
Command line calls are now supported.
python -m timeit_compare -s "n = 100" "(1 + n) * n / 2" "sum(range(1, n + 1))"
Run the following command for help.
python -m timeit_compare -h
Contact
If you have any suggestions, please contact me at 23S112099@stu.hit.edu.cn.
End
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file timeit_compare-1.1.1.tar.gz
.
File metadata
- Download URL: timeit_compare-1.1.1.tar.gz
- Upload date:
- Size: 8.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.12.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 547df4797c8b6154b5838a0f38ea0cdbdc69e1eb2374872755a7ecf0682f5659 |
|
MD5 | c93b49937b456539d8781dc729795221 |
|
BLAKE2b-256 | cffdb285296fc61e450391255dea7cd4316c07bb96aa95d3ea55a4c45b393d8d |