[TIP] Interesting getattr pattern [was: Re: [issue5728] Support telling TestResult objects a test run has finished]
Raphael Marvie
raphael.marvie at lifl.fr
Sat Apr 11 10:35:31 PDT 2009
John Arbash Meinel wrote:
[snip.]
> 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.
Including its definition which should be done only once.
> 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
The object() call is an instanciation, so it may cost more than an empty
function (but I may be wrong).
The goal of the noop() was to remove the test, so I would have compared:
$ python -mtimeit -s'x = None' 'if x is None: pass'
1000000 loops, best of 3: 0.208 usec per loop
$ python -mtimeit -s'y = object()'
10000000 loops, best of 3: 0.0906 usec per loop
which seems to encourage a the use of a noop() compare to a test if I
follow you benchmark.
r.
--
Raphael Marvie, PhD http://www.lifl.fr/~marvie/
Maître de Conférences / Associate Professor @ LIFL -- USTL
Directeur du Master Informatique Professionnel spécialité IAGL
Head of Master's in Software Engineering +33 3 20 33 59 51
More information about the testing-in-python
mailing list