[TIP] Interesting getattr pattern [was: Re: [issue5728] Support telling TestResult objects a test run has finished]
John Arbash Meinel
john at arbash-meinel.com
Sat Apr 11 09:08:59 PDT 2009
-----BEGIN PGP SIGNED MESSAGE-----
John Arbash Meinel wrote:
> Raphael Marvie wrote:
>> In fact my question was, would any one consider the proposal I made a
>> bad choice regarding performance. I am most often not limited by
>> performance issues. I just like to avoid tests when I can use some kind
>> of polymorphism, which I find cleaner to read. I'll remember the noop
>> name instead of donothing.
> I'll mention that on top of the time to create a closure (create the
> lambda/named function), you also encounter function call overhead. From
> all of the profiling tests I've done, a conditional is *much* cheaper
> than a no-op function call. AIUI, any function call has to create a
> frame, and those take longer than you'd like.
I don't know if this is 100% valid but:
$ python -mtimeit -s'x = None' 'x is None'
10000000 loops, best of 3: 0.0497 usec per loop
$ python -mtimeit -s'y = object()' 'y is None'
10000000 loops, best of 3: 0.0493 usec per loop
$ python -mtimeit -s'def noop(): pass' 'noop()'
10000000 loops, best of 3: 0.15 usec per loop
So at least according to that, a no-op function call is 3x slower than
an 'is None' check.
Now maybe it is a bit more fair to add an if() check on that:
$ python -mtimeit -s'x = None' 'if x is None: pass'
10000000 loops, best of 3: 0.0486 usec per loop
$ python -mtimeit -s'y = object()' 'if y is None: pass'
10000000 loops, best of 3: 0.0512 usec per loop
hmm.... the time seems to have improved? Anyway, no-op functions are
quite a bit slower than an 'is None' check. So if you make a habit of
checking 'is None' things go a bit faster.
I'll also note that just doing "if x: pass" is ever so slightly faster
than "if x is None:"
10000000 loops, best of 3: 0.0394 usec per loop
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (Cygwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
-----END PGP SIGNATURE-----
More information about the testing-in-python