Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #92240

Re: Function to show time to execute another function

From Cecil Westerhof <Cecil@decebal.nl>
Newsgroups comp.lang.python
Subject Re: Function to show time to execute another function
Organization Decebal Computing
References <87k2vgowco.fsf@Equus.decebal.nl> <55741414$0$12980$c3e8da3$5496439d@news.astraweb.com>
Date 2015-06-07 14:27 +0200
Message-ID <87twujoga1.fsf@Equus.decebal.nl> (permalink)

Show all headers | View raw


On Sunday  7 Jun 2015 11:51 CEST, Steven D'Aprano wrote:

> On Sun, 7 Jun 2015 04:39 pm, Cecil Westerhof wrote:
>
>> Sometimes I just want to know how much time a function takes, but
>> at the same time I also want the result of the function. For this I
>> wrote the following function: def time_test(function, *args):
>> startTime = time.time() results = function(*args) endTime =
>> time.time() print('It took {0} seconds'.format(endTime -
>> startTime)) return results
>
> Oh, I forgot to mention... 
>
> This has the disadvantage that it only prints the time, you cannot
> collect it for further processing. (Well, not easily -- you could
> always shadow the print built-in, then parse the string... ugggh,
> that's horrible.)

I already found this an omission and posted a better version.


> Using the Timer with statement I linked to earlier, you can easily
> collect both the time and the calculated result:
>
> with Timer(verbose=False) as t:
> do_this()
> do_that()
> result = something_else()
>
> time_taken = t.interval
> print(result, "took", time_taken)

Personally I find:
    time_test(test_random, (100, 10 ** 5))
better as:
    with Timer() as t:
        test_random(100, 10 ** 5)
also because I will do it in a loop with different values for the
second (and maybe the first) parameter. But that is probably personal
preference.


> If you are interested, I have a more advanced version of that Timer
> which I use almost every day. It works in Python 2.4 through 3.3, I
> expect it to work in 3.4 and 3.5 as well, and is extensively tested
> in CPython and seems to work in Jython and IronPython as well.

I would certainly be interested. I work with 3.4, so I could test it
for you. And maybe even build a 3.5 version and test it there also.

-- 
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 08:39 +0200
  Re: Function to show time to execute another function Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-07 08:39 +0100
    Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 10:22 +0200
      Re: Function to show time to execute another function Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-06-07 11:06 +0200
        Is it a newsgroup or a list? random832@fastmail.us - 2015-06-07 07:20 -0400
          Re: Is it a newsgroup or a list? Steven D'Aprano <steve@pearwood.info> - 2015-06-07 21:45 +1000
            Re: Is it a newsgroup or a list? Chris Warrick <kwpolska@gmail.com> - 2015-06-07 15:10 +0200
            Re: Is it a newsgroup or a list? Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-06-07 16:12 +0200
        Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 13:53 +0200
        Re: Is it a newsgroup or a list? Tim Golden <mail@timgolden.me.uk> - 2015-06-07 14:57 +0100
      Re: Function to show time to execute another function Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-07 11:16 +0100
        [OT] Re: Function to show time to execute another function Marko Rauhamaa <marko@pacujo.net> - 2015-06-07 14:02 +0300
          Re: [OT] Re: Function to show time to execute another function Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-06-07 12:12 +0100
        Re: Function to show time to execute another function Steven D'Aprano <steve@pearwood.info> - 2015-06-07 21:29 +1000
        Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 14:03 +0200
      Re: Function to show time to execute another function Tim Golden <mail@timgolden.me.uk> - 2015-06-07 12:05 +0100
        Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 14:43 +0200
      Re: Function to show time to execute another function Laura Creighton <lac@openend.se> - 2015-06-07 15:03 +0200
      Re: Function to show time to execute another function Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-06-07 20:51 +0200
        Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 22:35 +0200
          Re: Function to show time to execute another function Johannes Bauer <dfnsonfsduifb@gmx.de> - 2015-06-08 07:04 +0200
            Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-08 08:32 +0200
  Re: Function to show time to execute another function Steven D'Aprano <steve@pearwood.info> - 2015-06-07 19:28 +1000
    Re: Function to show time to execute another function Luca Menegotto <otlucaDELETE@DELETEyahoo.it> - 2015-06-07 11:44 +0200
    Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 14:14 +0200
    Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 14:58 +0200
  Re: Function to show time to execute another function Steven D'Aprano <steve@pearwood.info> - 2015-06-07 19:51 +1000
    Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 14:27 +0200
  Re: Function to show time to execute another function Cecil Westerhof <Cecil@decebal.nl> - 2015-06-07 11:31 +0200
  Re: Is it a newsgroup or a list? Gene Heskett <gheskett@wdtv.com> - 2015-06-07 11:49 -0400
  Re: Is it a newsgroup or a list? Larry Martell <larry.martell@gmail.com> - 2015-06-07 11:56 -0400

csiph-web