python time measurement

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()

Recommended Posts

python time measurement
Python execution time measurement memo
Execution time measurement with Python With
First time python
measurement of time
First time python
python> Processing time measurement> time.time () --start_time
Python: Time Series Analysis
Function execution time (Python)
Python time series question
Output python execution time
Time floor function (Python)
Measurement of execution time
python get current time
Use Measurement Protocol in Python
Python
Calculation time measurement using maf
Time measurement using a clock
Simple FPS measurement of python
[Python] Plot time series data
Time synchronization (Windows) with Python
Python: Time Series Analysis: Preprocessing Time Series Data
Measure function execution time in Python
python sql statement extracted by time
Python (from first time to execution)
Time series plot started ~ python edition ~
See python for the first time
Code tests around time in Python
kafka python
Python Date / Time Library Reverse Lookup Reference
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python FlowFishMaster
Python service
python tips
python function ①
Python basics
[Python] Matrix multiplication processing time using NumPy
Python memo
ufo-> python (3)
Python comprehension
install python
Python basics ④
Python Memorandum 2
python memo
Python Jinja2
Python increment
atCoder 173 Python
[Python] function
Just print Python elapsed time in seconds
Python installation
Python Master RTA for the time being
python tips
Installing Python 3.4.3.
Python memo
Python iterative
Python algorithm