I have two code snippets to time a function object being executed. I
expected that they should give roughly the same result, but one is more
than an order of magnitude slower than the other.
Here are the snippets:
def timer1():
timer = time.time
func = lambda : None
itr = [None] * 1000000
t0 = timer()
for _ in itr:
func()
t1 = timer()
return t1 - t0
def timer2():
timer = time.time
func = lambda : None
itr = [None] * 1000000
t = 0.0
for _ in itr:
t0 = timer()
func()
t1 = timer()
t += t1 - t0
return t
Here are the results: timer1()
0.54168200492858887 timer2()
6.1631934642791748
Of course I expect timer2 should take longer to execute in total,
because it is doing a lot more work. But it seems to me that all that
extra work should not affect the time measured, which (I imagine) should
be about the same as timer1. Possibly even less as it isn't timing the
setup of the for loop.
Any ideas what is causing the difference? I'm running Python 2.3 under
Linux.
Thanks,
--
Steven. 7 1148
Steven D'Aprano wrote: I have two code snippets to time a function object being executed. I expected that they should give roughly the same result, but one is more than an order of magnitude slower than the other.
Here are the snippets:
def timer1(): timer = time.time func = lambda : None itr = [None] * 1000000 t0 = timer() for _ in itr: func() t1 = timer() return t1 - t0
def timer2(): timer = time.time func = lambda : None itr = [None] * 1000000 t = 0.0 for _ in itr: t0 = timer() func() t1 = timer() t += t1 - t0 return t
Here are the results:
timer1() 0.54168200492858887 timer2()
6.1631934642791748
Of course I expect timer2 should take longer to execute in total, because it is doing a lot more work. But it seems to me that all that extra work should not affect the time measured, which (I imagine) should be about the same as timer1. Possibly even less as it isn't timing the setup of the for loop.
Any ideas what is causing the difference? I'm running Python 2.3 under Linux.
Thanks,
Steven,
In timer2 you are making a million extra calls to timer()
function and doing a million additions and a million
subtractions that are not being done in timer1() function.
I think that is where your "extra" time is located.
-Larry Bates
Steven D'Aprano wrote: I have two code snippets to time a function object being executed. I expected that they should give roughly the same result, but one is more than an order of magnitude slower than the other.
Here are the snippets:
def timer1(): timer = time.time func = lambda : None itr = [None] * 1000000 t0 = timer() for _ in itr: func() t1 = timer() return t1 - t0
def timer2(): timer = time.time func = lambda : None itr = [None] * 1000000 t = 0.0 for _ in itr: t0 = timer() func() t1 = timer() t += t1 - t0 return t
Here are the results:
timer1() 0.54168200492858887 timer2()
6.1631934642791748
Of course I expect timer2 should take longer to execute in total, because it is doing a lot more work. But it seems to me that all that extra work should not affect the time measured, which (I imagine) should be about the same as timer1. Possibly even less as it isn't timing the setup of the for loop.
Any ideas what is causing the difference? I'm running Python 2.3 under Linux.
You are including the cost of one call to timer() in each loop: The cost
of returning the time from the first call to timer() and the the cost of
getting the time in the second call. On my computer with Python 2.4:
D:\Projects\CB>python -m timeit -s "import time;timer=time.time" "t=timer()"
1000000 loops, best of 3: 0.498 usec per loop
which is almost exactly the same as the difference between timer1() and
timer2() on my machine:
timer1 0.30999994278
timer2 0.812000274658
using Python 2.4.2 on Win2k.
Kent
On Thu, 23 Feb 2006 10:07:14 -0600, Larry Bates wrote: Of course I expect timer2 should take longer to execute in total, because it is doing a lot more work. But it seems to me that all that extra work should not affect the time measured, which (I imagine) should be about the same as timer1. Possibly even less as it isn't timing the setup of the for loop.
.... In timer2 you are making a million extra calls to timer() function and doing a million additions and a million subtractions that are not being done in timer1() function. I think that is where your "extra" time is located.
But those extra additions, subtractions, etc. are not being timed, because
they are outside the part where the stopwatch is ticking, in a manner of
speaking.
I know that timer2 will take longer to execute than timer1. That's not
the surprising bit. But I'm not measuring all the execution time, only
part of it, and not all that execution time is being timed.
The only extra work I can see is a call to timer() each loop. The first
call shouldn't count, because it happens before the clock starts. Is
calling time.time() really such as expensive operation, especially when
I've rebound the name so as to avoid a name lookup?
I guess it is: I've run two more tests, both going back to the form of
timer1, but changing the function being timed. The first uses math.sin.
The second uses time.timer.
Here is the code:
def timer3():
timer = time.time
func = math.sin
itr = [None] * 1000000
t0 = timer()
for _ in itr:
func(0.1)
t1 = timer()
return t1 - t0
def timer4():
timer = time.time
func = timer
itr = [None] * 1000000
t0 = timer()
for _ in itr:
func()
t1 = timer()
return t1 - t0
And the results: timer.timer3()
0.64760088920593262 timer.timer4()
5.2274949550628662
It looks like the time function under Linux at least is very slow.
--
Steven.
Steven D'Aprano wrote: It looks like the time function under Linux at least is very slow.
Perhaps you should try doing the same thing in C.
Then you can see whether the problem is in the
wrapper or in the system call.
On Fri, 24 Feb 2006 10:11:18 +0100, Magnus Lycka wrote: Steven D'Aprano wrote: It looks like the time function under Linux at least is very slow.
Perhaps you should try doing the same thing in C. Then you can see whether the problem is in the wrapper or in the system call.
Good idea.
Now, has anyone got a copy of "Learning C for Dummies" I can borrow?
*chagrined smile*
--
Steven.
Steven D'Aprano schreef: On Fri, 24 Feb 2006 10:11:18 +0100, Magnus Lycka wrote:
Steven D'Aprano wrote: It looks like the time function under Linux at least is very slow. Perhaps you should try doing the same thing in C. Then you can see whether the problem is in the wrapper or in the system call.
Good idea.
Now, has anyone got a copy of "Learning C for Dummies" I can borrow?
*chagrined smile*
First, for reference, the Python version on the machine I used for
testing (Debian 3.1, P4 3GHz):
timer1: 0.36007809639
timer2: 3.18800234795
On Windows the difference is much less.
C (compiled with gcc 3.3.5 without optimizations) (see below for the code):
timer1: 0.000603
timer2: 2.624557
I think it looks like the problem is in the system call.
C code:
#include <stdio.h>
#include <sys/time.h>
#include <time.h>
void func(void)
{
}
double gettime()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec + tv.tv_usec/1e6;
}
double timer1(void)
{
double t0, t1;
int i;
t0 = gettime();
for (i = 0; i < 100000; ++i)
{
func();
}
t1 = gettime();
return t1 - t0;
}
double timer2(void)
{
int i;
double t = 0.0;
double t0, t1;
for (i = 0; i < 1000000; ++i)
{
t0 = gettime();
func();
t1 = gettime();
t += t1 - t0;
}
return t;
}
int main(void)
{
printf("timer1: %lf\n", timer1());
printf("timer2: %lf\n", timer2());
return 0;
}
--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton
Roel Schroeven
On Fri, 24 Feb 2006 10:51:09 +0000, Roel Schroeven wrote:
[snip] I think it looks like the problem is in the system call.
Thank you Roel for going the extra mile. I appreciate the unexpected bonus.
--
Steven. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Shabam |
last post by:
Ok I'm trying to run a php script written by someone else, not me, and it's
getting stuck in a particular step. Actually it isn't getting stuck per se,
but the browser is, because it's taking...
|
by: Eric Anderson Vianet SAO |
last post by:
hello all
When i tried ´pg_dump -v -f dump.dmp dtbtransporte´ I got the error:
pg_dump: restoring data for table tbdmovimento
pg_dump: dumping out the contents of table tbdmovimento
...
|
by: Sean |
last post by:
I have a struct that I wrote to test a protocol. The idea I had was
to just declare the elements of the struct in the order in which they
are sent and received as defined by the protocol. ...
|
by: Reiner Apke |
last post by:
Hello,
I have got a very strange problem with the calcualtion of the the square
root (Math.Sqrt()).
I calculate in a loop a lot of of diameters
maxDiameter = Math.Sqrt(maxCrossSection *...
|
by: Photubias |
last post by:
I get the above error when issueing the 'db2start' command.
Found no info in the net that could help me. I have the V8.1 FP10 installed
on SLES 9 x86_64
Any tips?
Tx in advance
|
by: Steven D'Aprano |
last post by:
The timeit module is ideal for measuring small code snippets; I want to
measure large function objects.
Because the timeit module takes the code snippet argument as a string, it
is quite handy...
|
by: Steven D'Aprano |
last post by:
After reading an earlier thread about opening and closing lots of files,
I thought I'd do a little experiment.
Suppose you have a whole lot of files, and you need to open each one,
append a...
|
by: Daniel Fetchinson |
last post by:
On 4/15/08, Daniel Fetchinson <fetchinson@googlemail.comwrote:
BTW, using the following
######################################################################
# CODE TO TEST BOTH...
|
by: bintom |
last post by:
I ran the following simple code in C++ and got unexpected results:
float f = 139.4;
cout << f;
Output:
139.399994;
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |