3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

python 時間計測

Last updated at Posted at 2013-05-27
laptimer.py
# -*- encoding: utf-8 -*-

__version__ = '0.0.1'


from time import time, sleep

#---------------------------------------------------------------
# LapTimer CLASS
#---------------------------------------------------------------
class LapTimer(object):

    """
    For example:

        from laptimer import LapTimer

        timer = LapTimer("TEST SCRIPT")

        timer.lap("start script")
        for i in xrange(3):
            sleep(i+1)
            timer.lap('sleep time %d sec' % (i+1))

        timer.get_lap()

    Returns:

        -------------------------
        TEST SCRIPT
           Lap : Total : message
        -------------------------
         0.000 : 0.000 : start script
         1.002 : 1.002 : sleep time 1 sec
         2.003 : 3.005 : sleep time 2 sec
         3.004 : 6.009 : sleep time 3 sec

    """

    def __init__(self, name = None):

        self.start_time = self.now_time = self.last_time = time()
        self.name       = name
        self.time_list  = []

    def lap(self, message=None):

        self.now_time   = time()
        self.lap_time   = self.now_time - self.last_time
        self.total_time = self.now_time - self.start_time
        time_list       = (self.lap_time, self.total_time, message)
        self.time_list.append(time_list)
        self.last_time = self.now_time
        return ' %5.3f - %5.3f - %s' % time_list

    def get_lap(self):

        print '-' * 25
        print '%s' % self.name
        print ' %5s : %5s : message' % ('Lap', 'Total')
        print '-' * 25
        for i in self.time_list:
            print ' %5.3f : %5.3f : %s' % i

#---------------------------------------------------------------
# TEST
#---------------------------------------------------------------
def test():

    '''
    TEST

    '''

    timer = LapTimer("TEST SCRIPT")

    timer.lap("start script")
    for i in xrange(3):
        sleep(i+1)
        timer.lap('sleep time %d sec' % (i+1))

    timer.get_lap()

if __name__ == '__main__': test()
3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?