Skip to main content

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for timeit2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 23ec59e5b632f3abd5d2949206cf73ab99fc1d983943d2affb9af3b61f4c8668
MD5 f9c207f319275626304b6f9a6ec3b088
BLAKE2b-256 0c1074dcaa89be296fbad04cfe243d26885d65551a75b14b2204e0825777cdb8

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for timeit2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b5502c56788cbb8f3d9ecb63cb5e5fb252c21daeecc52ce16aa1248b728a92e3
MD5 5605b97f24d7b186f4f1215324294fc8
BLAKE2b-256 ae38f42302f394bf1005fe13095356d5047430317701309bb17ee4bb76354c87

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