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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for timeit2-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9f084341febbd0596742af20410fd64fd8f9f1da71d0b4e019dd1d54cb8c1a33
MD5 041dbb822377d71fdb1a434050bf9aa6
BLAKE2b-256 41f18d506a9f92db979bb86f0ca738b43ddf02dbf7eae65900ccf31de89712d4

See more details on using hashes here.

File details

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

File metadata

  • Download URL: timeit2-0.1.1-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.6 Darwin/18.7.0

File hashes

Hashes for timeit2-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 106c287814c949b3065461b0fb9b8f71ddeecc7f60488889bf8105131ab7f97e
MD5 26dbb764d69803c2667a1951273d92db
BLAKE2b-256 3f64ca1107db1efec81ce290bd5a884e2a7ac416fd4d9efff41e9cce181c2220

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