Skip to main content

Based on the timeit library, timeit_compare can time multiple statements and provide comparison results.

Project description

timeit_compare

Based on the timeit library, timeit_compare can time multiple 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.

You can also perform the following operations to obtain specific values:

from timeit_compare import Compare

cmp = Compare()
for func in sum1, sum2, sum3, sum4, sum5:
    cmp.add_timer(func)
cmp.run()
# cmp.print_results()

result = cmp.get_result(0)  # get the result of the timer with index 0
print(
    result.time, result.repeat, result.number,
    result.mean, result.median, result.min, result.max, result.std,
    result.error, sep='\n'
)
# [3.3324892420678126e-06, 3.175742677501652e-06, 3.2153616044376503e-06, 3.267199346172507e-06, 3.2638434747711383e-06]
# 5
# 24405
# 3.2509272689901518e-06
# 3.2638434747711383e-06
# 3.175742677501652e-06
# 3.3324892420678126e-06
# 5.916418598334957e-08
# None

fastest = cmp.get_fastest()  # get the result of the fastest timer
print(fastest.index)  # 4

In a command line interface, call as follows:

python -m timeit_compare -s "n = 100" "s = 0;for i in range(1, n + 1):;    s += i" "sum(range(1, n + 1))" "(1 + n) * n // 2"

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


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.2.2.tar.gz (9.7 kB view details)

Uploaded Source

File details

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

File metadata

  • Download URL: timeit_compare-1.2.2.tar.gz
  • Upload date:
  • Size: 9.7 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.2.2.tar.gz
Algorithm Hash digest
SHA256 929051b8d617f4ff6ce32e09b130d876945f34b1d3785b2c554627b4b42d6cbb
MD5 a75af4096ac4faa7e5679cb0760dbe37
BLAKE2b-256 1a9787b49b45d5cf85614326e672f4a8230fa503f22d198d4447405caf064781

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