[BangPypers] Place the timer in Python script

Naresh Khalasi nkhalasi at vayana.in
Fri Sep 18 13:52:43 CEST 2009


I used this when I first explored....but now I prefer "timeit"

        from datetime import datetime as dt
        session = Session()
        try:
            log.debug('Creating all enterprise users at once')
            start_time = dt.now()
            session.add_all(list_of_users)
            session.commit()
            end_time = dt.now()
            log.debug('Time taken to store data in database: %s' %(end_time
- start_time))
        except IntegrityError, interr:
            session.rollback()
            return ['Problems storing the user data']

You won't have to format the difference. It gives you in hh:mm:ss:milisec
format.


On Fri, Sep 18, 2009 at 4:02 PM, Vishal <vsapre80 at gmail.com> wrote:

> On Fri, Sep 18, 2009 at 3:43 PM, steve <steve at lonetwin.net> wrote:
>
>> On 09/18/2009 03:33 PM, Vamsi wrote:
>>
>>> How to place the timer in python code.
>>>
>>> I want calculate the python script execution time.
>>>
>>> ex : Start Timer
>>>      : End Timer
>>>
>>> Execution Time : End Timer - Start Timer.
>>>
>>> How can we write the python code for this.
>>>
>> http://docs.python.org/library/debug.html
>>
>> Specifically, the timeit module
>> http://docs.python.org/library/timeit.html
>>
>> btw, dunno if you've heard about this really neat web application. It is a
>> simple text box with a couple of buttons that answers questions such as
>> these. I forgot it's name. It's something like golgol or some such.
>>
>> cheers,
>> - steve
>>
>> --
>> random non tech spiel: http://lonetwin.blogspot.com/
>> tech randomness: http://lonehacks.blogspot.com/
>> what i'm stumbling into: http://lonetwin.stumbleupon.com/
>>
>> _______________________________________________
>> BangPypers mailing list
>> BangPypers at python.org
>> http://mail.python.org/mailman/listinfo/bangpypers
>>
>
>
> Here's what I usually do on windows:
>
> from time import clock, strftime, gmtime
>
> start = clock()
>
> ... do you work here...
>
> end = clock()
>
> elapsed = end - start
> print "Time Taken to finish: %s" % (strftime("%Hhrs: %Mmins: %ssecs",
> gmtime(elapsed))
>
> If you wish to record in miliseconds, you'll have to do some math on the
> 'elapsed' value.
>
>
> --
> Thanks and best regards,
> Vishal Sapre
>
> ---
>
> "So say...Day by day, in every way, I am getting better, better and better
> !!!"
> "A Strong and Positive attitude creates more miracles than anything else.
> Because...Life is 10% how you make it, and 90% how you take it"
> "Diamond is another piece of coal that did well under pressure”
> "Happiness keeps u Sweet, Trials keep u Strong,
> Sorrow keeps u Human, Failure Keeps u Humble,
> Success keeps u Glowing, But only God Keeps u Going.....Keep Going....."
>
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/bangpypers/attachments/20090918/4ee19e12/attachment.htm>


More information about the BangPypers mailing list