Skip to main content

An easy-to-use algorithms timer.

Project description

Algorithm Timer

Overview

An easy-to-use algorithm timer.

Mechanism

We use a context-manager and with in Python to give an convinent way to test a specific block of code. Just see the following examples.

Note that we design this plot function here to test some algorithms' runing time and you can use it to test(and plot) the time of any block of code with minor change in source code(the TimerPloter class, specifically)

Eaxmples

Fibnacci

from algotimer import Timer, TimerPloter


def fib(n):
    if n <= 2:
        return 1
    return fib(n - 1) + fib(n - 2)

def fibMemo(n):
    cache = {1: 1, 2: 1}

    def rec(n):
        if n not in cache:
            cache[n] = rec(n - 1) + rec(n - 2)
        return cache[n]
    return rec(n)


if __name__ == '__main__':
    with Timer('fib, 30') as t:
        print('fib(30) = ', fib(30))

    with Timer('fib, 35') as t:
        print('fib(35) = ', fib(35))

    with Timer('fibMemo, 30') as t:
        print('fibMemo(30) = ', fibMemo(30))

    with Timer('fibMemo, 35') as t:
        print('fibMemo(35) = ', fibMemo(35))

    ploter = TimerPloter()
    ploter.plot()

The output:

fib(30) =  832040
fib, 30 Spends 0.217 s
fib(35) =  9227465
fib, 35 Spends 2.434 s
fibMemo(30) =  832040
fibMemo, 30 Spends 0.0 s
fibMemo(35) =  9227465
fibMemo, 35 Spends 0.0 s

And we get two files: logging,csv is the time data.

fib, 30, 0.217
fib, 35, 2.434
fibMemo, 30, 0.0
fibMemo, 35, 0.0

And Timer.png, a plot of the data.

Classification

from algotimer import Timer, TimerPloter
from sklearn import datasets
from sklearn.naive_bayes import GaussianNB
from sklearn.neighbors import KNeighborsClassifier

iris = datasets.load_iris()

with Timer('GaussianNB, Train'):
    gnb = GaussianNB()
    clf = gnb.fit(iris.data, iris.target)

with Timer('GaussianNB, Test'):
    y_pred = clf.predict(iris.data)
    print("Number of mislabeled points out of a total %d points : %d"
        % (iris.data.shape[0], (iris.target != y_pred).sum()))

with Timer('KNN(K=3), Train'):
    neigh = KNeighborsClassifier(n_neighbors=3)
    clf = neigh.fit(iris.data, iris.target) 

with Timer('KNN(K=3), Test'):
    y_pred = clf.predict(iris.data)
    print("Number of mislabeled points out of a total %d points : %d"
        % (iris.data.shape[0], (iris.target != y_pred).sum()))

with Timer('KNN(K=5), Train'):
    neigh = KNeighborsClassifier(n_neighbors=5)
    clf = neigh.fit(iris.data, iris.target) 

with Timer('KNN(K=5), Test'):
    y_pred = clf.predict(iris.data)
    print("Number of mislabeled points out of a total %d points : %d"
        % (iris.data.shape[0], (iris.target != y_pred).sum()))

# plot it
ploter = TimerPloter()
ploter.plot()

The output:

GaussianNB, Train Spends 0.001 s
Number of mislabeled points out of a total 150 points : 6
GaussianNB, Test Spends 0.001 s
KNN(K=3), Train Spends 0.019 s
Number of mislabeled points out of a total 150 points : 6
KNN(K=3), Test Spends 0.019 s
KNN(K=5), Train Spends 0.001 s
Number of mislabeled points out of a total 150 points : 5
KNN(K=5), Test Spends 0.01 

File logging.csv:

GaussianNB, Train, 0.001
GaussianNB, Test, 0.001
KNN(K=3), Train, 0.019
KNN(K=3), Test, 0.019
KNN(K=5), Train, 0.001
KNN(K=5), Test, 0.01

File Timer.png

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

algo-timer-0.0.3.tar.gz (3.2 kB view details)

Uploaded Source

Built Distribution

algo_timer-0.0.3-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file algo-timer-0.0.3.tar.gz.

File metadata

  • Download URL: algo-timer-0.0.3.tar.gz
  • Upload date:
  • Size: 3.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.6.8

File hashes

Hashes for algo-timer-0.0.3.tar.gz
Algorithm Hash digest
SHA256 3da0488fe6af84adf4501863ec5bcee5a5a3fdbcfa2d649abcda071313e7a635
MD5 543803d50a744e4ddc8dca5a630231b1
BLAKE2b-256 a7a0ae9c10b4ff923b78372f56bd82191a63a9712bc0288a953d59b369f174b0

See more details on using hashes here.

File details

Details for the file algo_timer-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: algo_timer-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 4.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.6.8

File hashes

Hashes for algo_timer-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 cc1e3cb0fae1d21351e6199a7cd293b3a89cee92a5eed1dd4b5b06a264eb4348
MD5 6f90c6e87b1d463ec05bfd153bc4e26f
BLAKE2b-256 bd3582e9ae6124b21493a23aab6245a41c764d8e73a27990d9c9071dcb10a485

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