{"id":17030248,"url":"https://github.com/shenxiangzhuang/algo-timer","last_synced_at":"2026-05-10T19:33:57.566Z","repository":{"id":57409653,"uuid":"223513953","full_name":"shenxiangzhuang/algo-timer","owner":"shenxiangzhuang","description":"An easy-to-use algorithms timer.","archived":false,"fork":false,"pushed_at":"2019-11-23T07:11:11.000Z","size":2180,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T06:34:22.860Z","etag":null,"topics":["python3","timer-application"],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/shenxiangzhuang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-11-23T01:32:53.000Z","updated_at":"2019-11-23T07:11:13.000Z","dependencies_parsed_at":"2022-08-24T20:00:17.418Z","dependency_job_id":null,"html_url":"https://github.com/shenxiangzhuang/algo-timer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenxiangzhuang%2Falgo-timer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenxiangzhuang%2Falgo-timer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenxiangzhuang%2Falgo-timer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/shenxiangzhuang%2Falgo-timer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/shenxiangzhuang","download_url":"https://codeload.github.com/shenxiangzhuang/algo-timer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245015883,"owners_count":20547489,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["python3","timer-application"],"created_at":"2024-10-14T08:05:25.761Z","updated_at":"2026-05-10T19:33:52.517Z","avatar_url":"https://github.com/shenxiangzhuang.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"![](logo.png)\n\n# Algorithm Timer\n\n\n## Overview\n\nAn easy-to-use algorithm timer.\n\n\n## Install\n\n[algo-timer in pypi](https://pypi.org/project/algo-timer/).\n\nYou can install `algotimer` with `pip`:\n\u003epip install algo-timer\n\nNote we use `pandas` `matplotlib`, and `seaborn` to plot the figure, so make sure you have\ninstalled these packages in you environment before you use `algotimer`.\n\n## Mechanism\nWe use a context-manager and `with` in Python to give an convinent way to test\na specific block of code. Just see the following examples.\n\nNote 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)\n\n## Eaxmples\n\n### Fibnacci\n\n```python\nfrom algotimer import Timer, TimerPloter\n\n\ndef fib(n):\n    if n \u003c= 2:\n        return 1\n    return fib(n - 1) + fib(n - 2)\n\ndef fibMemo(n):\n    cache = {1: 1, 2: 1}\n\n    def rec(n):\n        if n not in cache:\n            cache[n] = rec(n - 1) + rec(n - 2)\n        return cache[n]\n    return rec(n)\n\n\nif __name__ == '__main__':\n    with Timer('fib, 30') as t:\n        print('fib(30) = ', fib(30))\n\n    with Timer('fib, 35') as t:\n        print('fib(35) = ', fib(35))\n\n    with Timer('fibMemo, 30') as t:\n        print('fibMemo(30) = ', fibMemo(30))\n\n    with Timer('fibMemo, 35') as t:\n        print('fibMemo(35) = ', fibMemo(35))\n\n    ploter = TimerPloter()\n    ploter.plot()\n\n```\nThe output:\n```\nfib(30) =  832040\nfib, 30 Spends 0.217 s\nfib(35) =  9227465\nfib, 35 Spends 2.434 s\nfibMemo(30) =  832040\nfibMemo, 30 Spends 0.0 s\nfibMemo(35) =  9227465\nfibMemo, 35 Spends 0.0 s\n```\nAnd we get two files:\n`logging,csv` is the time data.\n\n```\nfib, 30, 0.217\nfib, 35, 2.434\nfibMemo, 30, 0.0\nfibMemo, 35, 0.0\n```\n\nAnd `Timer.png`, a plot of the data.\n![](https://github.com/shenxiangzhuang/algo-timer/raw/master/examples/fibnacci/Timer.png)\n\n\n\n\n### Classification\n```python\nfrom algotimer import Timer, TimerPloter\nfrom sklearn import datasets\nfrom sklearn.naive_bayes import GaussianNB\nfrom sklearn.neighbors import KNeighborsClassifier\n\niris = datasets.load_iris()\n\nwith Timer('GaussianNB, Train'):\n    gnb = GaussianNB()\n    clf = gnb.fit(iris.data, iris.target)\n\nwith Timer('GaussianNB, Test'):\n    y_pred = clf.predict(iris.data)\n    print(\"Number of mislabeled points out of a total %d points : %d\"\n        % (iris.data.shape[0], (iris.target != y_pred).sum()))\n\nwith Timer('KNN(K=3), Train'):\n    neigh = KNeighborsClassifier(n_neighbors=3)\n    clf = neigh.fit(iris.data, iris.target) \n\nwith Timer('KNN(K=3), Test'):\n    y_pred = clf.predict(iris.data)\n    print(\"Number of mislabeled points out of a total %d points : %d\"\n        % (iris.data.shape[0], (iris.target != y_pred).sum()))\n\nwith Timer('KNN(K=5), Train'):\n    neigh = KNeighborsClassifier(n_neighbors=5)\n    clf = neigh.fit(iris.data, iris.target) \n\nwith Timer('KNN(K=5), Test'):\n    y_pred = clf.predict(iris.data)\n    print(\"Number of mislabeled points out of a total %d points : %d\"\n        % (iris.data.shape[0], (iris.target != y_pred).sum()))\n\n# plot it\nploter = TimerPloter()\nploter.plot()\n```\n\nThe output:\n```\nGaussianNB, Train Spends 0.001 s\nNumber of mislabeled points out of a total 150 points : 6\nGaussianNB, Test Spends 0.001 s\nKNN(K=3), Train Spends 0.019 s\nNumber of mislabeled points out of a total 150 points : 6\nKNN(K=3), Test Spends 0.019 s\nKNN(K=5), Train Spends 0.001 s\nNumber of mislabeled points out of a total 150 points : 5\nKNN(K=5), Test Spends 0.01 \n```\n\nFile `logging.csv`:\n```\nGaussianNB, Train, 0.001\nGaussianNB, Test, 0.001\nKNN(K=3), Train, 0.019\nKNN(K=3), Test, 0.019\nKNN(K=5), Train, 0.001\nKNN(K=5), Test, 0.01\n```\n\nFile `Timer.png`\n![](https://github.com/shenxiangzhuang/algo-timer/raw/master/examples/classification/Timer.png)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenxiangzhuang%2Falgo-timer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fshenxiangzhuang%2Falgo-timer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fshenxiangzhuang%2Falgo-timer/lists"}