Git Product home page Git Product logo

pytimer's Introduction

pytimer

在模型训练或数据处理中,常常涉及代码段的计时,这里给出优雅的计时器实现。

例子一,作为上下文管理器,

import urllib.request as request
from pytimer import Timer

def task(n):
    for _ in range(n):
        request.urlopen("https://httpbin.org/")

with Timer(label="request url") as timer:
    task(5)

print(timer.elapsed)

例子二,作为装饰器,

import urllib.request as request
from pytimer import Timer

@Timer(label="time url request")
def task(n):
    for _ in range(n):
        request.urlopen("https://httpbin.org/")

task(1)

例子三,在类中使用,

import urllib.request as request
from pytimer import timethis

class Task:

    @timethis(label="time instance method")
    def urlopen1(self, url):
        request.urlopen(url)

    @classmethod
    @timethis(label="time class method")
    def urlopen2(self, url):
        request.urlopen(url)

    @staticmethod
    @timethis(label="time static method")
    def urlopen3(url):
        request.urlopen(url)

task = Task()
task.urlopen1("https://httpbin.org/")
task.urlopen2("https://httpbin.org/")
task.urlopen3("https://httpbin.org/")

pytimer's People

Contributors

allenwind avatar

Stargazers

zhifeng avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.