Skip to main content

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:

output_example.png

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

  1. 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)
    
  2. 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'
    )
    
  3. 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.

  4. 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
    

Release 1.1.2

  1. Added a parameter: time, which represents approximate total running time of all statements in seconds (default 1.0). When a positive value of number parameter is not given, it will be used to estimate number. Ignored when number is greater than 0.

  2. Corrected the error of representing sample standard deviation as population standard deviation.


Contact

If you have any suggestions, please contact me at 23S112099@stu.hit.edu.cn.


End

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

timeit_compare-1.1.2.tar.gz (8.1 kB view details)

Uploaded Source

File details

Details for the file timeit_compare-1.1.2.tar.gz.

File metadata

  • Download URL: timeit_compare-1.1.2.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for timeit_compare-1.1.2.tar.gz
Algorithm Hash digest
SHA256 786672ee9d844bbb1265443895c2345234b541722016ee542cf0deb8448d7d79
MD5 6b42206f5d7c8f9ac175de879f8bb871
BLAKE2b-256 5c03773d20fec54354053028dd35036f9ac0e2d4ddf245aebce31c29dd16406d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page