Skip to main content

extended version of timeit package

Project description

timeit2

このパッケージは処理時間の計測module timeit を使いやすくしたものです。以下の特徴があります。

  • 引数を使い回しません(timeitは引数を書き換える処理をした時にそのまま次に渡します)
  • 複数の関数を1度に計測して相対的な速度差を視覚化できます
  • 処理時間の桁数を変えられます(0.00374567より0.0037の方が見やすい)
  • 返り値を一緒にprintできます

test

Install

$ pip install timeit2

Example

from timeit2 import ti2

arg = range(10 ** 6)
ti2(
    max,
    sum,
    args=[arg],
    floor=4,
    print_return=True,
    return_label="ret: ",
    relative=True,
)
# max:
#         0.0356 sec
#         ret: 999999
# sum:
#         0.0249 sec
#         ret: 499999500000
# relative:
#         sum:
#                 1
#         max:
#                 1.43

最小限の使い方

from timeit2 import ti2

def f():
    li = []
    for i in range(10 ** 5):
        li.append(i * i)
    return li

ti2(f)

# f:
#         0.015094 sec

配列作成にlist.append,list.insert,dequeをそれぞれ使った処理時間と相対評価

from collections import deque
from timeit2 import ti2


def append_(n):
    li = []
    for i in range(n):
        li.append(i)
    return li


def insert_(n):
    li = []
    for i in range(n):
        li.insert(0, i)
    return li


def deque_right(n):
    li = deque()
    for i in range(n):
        li.append(i)
    return li


def deque_left(n):
    li = deque()
    for i in range(n):
        li.appendleft(i)
    return li


ti2(
    append_,
    insert_,
    deque_right,
    deque_left,
    args=[10 ** 4],
    relative=True,
)

# append_:
#         0.001118 sec
# insert_:
#         0.021187 sec
# deque_right:
#         0.000891 sec
# deque_left:
#         0.000855 sec
# relative:
#         deque_left:
#                 1
#         deque_right:
#                 1.04
#         append_:
#                 1.31
#         insert_:
#                 24.77

API

ti2(
    *fn: Callable,
    args: list = [],
    number: int = 100,
    floor: int = 6,
    relative: bool = False,
    print_return: bool = False,
    return_label: str = "",
)

argument

fn

Type: Callable

計測する関数、可変長引数なので複数指定が可能。 引数がある場合はargs= で指定する。

Options

args

Type: list

Default: []

fnに渡す引数のlist

number

Type: int

Default: 100

各関数を計測する回数、結果はその最速値が表示される

floor

Type: int

Default: 6

表示する小数点以下の桁数

relative

Type: bool

Default: False

計測結果に相対評価を追加する

print_return

Type: bool

Default: False

計測結果に関数の返り値を追加する

return_label

Type: str Default: ""

print_return=Trueの場合、返り値のラベルを指定する

alias

ti2, ti, timeit, timeit2 をimportできますが、全て同じものです。

from timeit2 import ti2, ti, timeit, timeit2

Related

License

MIT © 2021 atu4403

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

timeit2-1.0.0.tar.gz (3.7 kB view details)

Uploaded Source

Built Distribution

timeit2-1.0.0-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file timeit2-1.0.0.tar.gz.

File metadata

  • Download URL: timeit2-1.0.0.tar.gz
  • Upload date:
  • Size: 3.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.12 Darwin/18.7.0

File hashes

Hashes for timeit2-1.0.0.tar.gz
Algorithm Hash digest
SHA256 737344bc80eb06cbd03e84aa67355d21b166eb6d277cd5997c8b0e87a8f6b84a
MD5 eed748a241cd34c02a6cebfeb5496411
BLAKE2b-256 e414c2afd7b2e1ef3e5fb2fa56b885ed4f029ff7f611b757dbe955f5083babd5

See more details on using hashes here.

File details

Details for the file timeit2-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: timeit2-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.12 Darwin/18.7.0

File hashes

Hashes for timeit2-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 dd553c76177e844d0bea23e0a98d233a828f0e97e5ff4579915b63536e31b561
MD5 8ec1b3eaeff6f773176b863b9dbb7cd3
BLAKE2b-256 11a82aa7599e8d1f29cda32bed4c8de523e96993f91f531479d5839739e89c2c

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