Throw the cat among the pigeons
Dave Angel
davea at davea.name
Tue May 5 17:55:01 EDT 2015
On 05/05/2015 05:39 PM, Ian Kelly wrote:
> On Tue, May 5, 2015 at 3:23 PM, Ian Kelly <ian.g.kelly at gmail.com> wrote:
>> On Tue, May 5, 2015 at 3:00 PM, Dave Angel <davea at davea.name> wrote:
>>> def loop(func, funcname, arg):
>>> start = time.time()
>>> for i in range(repeats):
>>> func(arg, True)
>>> print("{0}({1}) took {2:7.4}".format(funcname, arg, time.time()-start))
>>>
>>> start = time.time()
>>> for i in range(repeats):
>>> func(arg)
>>> print("{0}({1}) took {2:7.4}".format(funcname, arg, time.time()-start))
>>
>> Note that you're explicitly passing True in one case but leaving the
>> default in the other. I don't know whether that might be responsible
>> for the difference you're seeing.
>
> I don't think that's the cause, but I do think that it has something
> to do with the way the timing is being run. When I run your loop
> function, I do observe the difference. If I reverse the order so that
> the False case is tested first, I observe the opposite. That is, the
> slower case is consistently the one that is timed *first* in the loop
> function, regardless of which case that is.
>
I created two functions and called them with Timeit(), and the
difference is now below 3%
And when I take your lead and double the loop() function so it runs each
test twice, I get steadily decreasing numbers.
I think all of this has been noise caused by the caching of objects
including function objects. I was surprised by this, as the loops are
small enough I'd figure the function object would be fully cached the
first time it was called.
--
DaveA
More information about the Python-list
mailing list