Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #92239
| 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> <55740ecf$0$12978$c3e8da3$5496439d@news.astraweb.com> |
| Date | 2015-06-07 14:14 +0200 |
| Message-ID | <87y4jvoguy.fsf@Equus.decebal.nl> (permalink) |
On Sunday 7 Jun 2015 11:28 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
>
>
> There is a lot of subtlety in timing functions on modern day
> computers. What you measure there includes the time taken to lookup
> the names "function" and "args" (although that ought to be very
> quick) and to expand out *args (not quite so quick).
>
> More importantly, it will also be less accurate on Windows systems,
> and may include time during which the operating system is running
> other background tasks. It makes no attempt to allow for whether
> code is in the CPU cache or not. And what if the garbage collector
> happens to run in the middle of your test?
>
> Timing code these days is subtle and complicated!
>
> Depending on what function actually does, those complications may,
> or may not, make a real difference. If function() does a lot of work
> (say, at least one second) then what I have said is probably
> completely irrelevant and you can ignore it. For those cases, your
> function is perfectly fine, although I prefer to use a with
> statement interactively. Of course I can still call a function, but
> I don't *have* to call a function.
Next time I should give more background. ;-)
This function is only mend to get an indication of the needed time and
in my opinion is only useful from ate least 5 seconds. If you want
real measurements you should use timeit I think.
> Here is a simple example:
>
> http://code.activestate.com/recipes/577896-benchmark-code-with-the-with-statement/
That looks quite interesting. I think I will use that in my function.
I still think the function is handy, because you need less code with
it.
> But if your function takes less than, say, 1 millisecond, then your
> timing results are probably just meaningless random numbers,
> affected more by the other ten thousand processes running on your
> computer than by the Python code itself.
>
> In that case, you should learn how to use the timeit module. It's a
> little complex, but worth it for timing small code snippets.
I knew that. It is just to use before using the heavy guns (and also
wanting the result of the function). But I should be more explicit
next time.
--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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