Login

Create breakpoints to time code at

Author:
menendez
Posted:
June 4, 2008
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

Include in your code like this:

t=Timer()

Then use it like this:

t.tick('Some optional description')

It will output the time spent between the tick and the previous tick (or inception) and the total time spent since it began tracking time. Can be placed multiple times in a long segment of code. Can be used to break out the amount of time being spent on various parts of your code so you can focus on optimizing those sections.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# By Ed and Rudy Menendez
import time
class Timer(object):
    def __init__(self):
        self.bot = self.last_time = time.time()
        print u'Starting timer at %s' % self.last_time

    def tick(self, msg='Timer'):
        x = time.time()
        print '%s: Since inception %.3f, since last call %.3f' % (msg, x-self.bot, x - self.last_time)
        self.last_time = x

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 1 week ago
  3. Serializer factory with Django Rest Framework by julio 10 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 3 weeks ago
  5. Help text hyperlinks by sa2812 11 months, 2 weeks ago

Comments

Please login first before commenting.