fastest way to detect a user type
Robin Becker
robin at NOSPAMreportlab.com
Sun Feb 1 07:33:36 EST 2009
Steven D'Aprano wrote:
> Paul Rubin wrote:
>
>> Steven D'Aprano <steve at pearwood.info> writes:
>>> First question is, why do you care that it's slower? The difference
>>> between the fastest and slowest functions is 1.16-0.33 = 0.83
>>> microsecond.
>> That's a 71% speedup, pretty good if you ask me.
>
> Don't you care that the code is demonstrably incorrect? The OP is
> investigating options to use in Python 3, but the fastest method will fail,
> because the "type is InstanceType" test will no longer work. (I believe the
> fastest method, as given, is incorrect even in Python 2.x, as it will
> accept ANY old-style class instead of just the relevant X or V classes.)
I'm not clear why this is true? Not all instances will have the __X__
attribute or has something else changed in Python3?
The original code was intended to be called with only a subset of all
class instances being passed as argument; as currently written it was
unsafe because an instance of an arbitrary old class would pass into
branch 1. Of course it will still be unsafe as arbitrary instances end
up in branch 3
The intent is to firm up the set of cases being accepted in the first
branch. The problem is that when all instances are new style then
there's no easy check for the other acceptable arguments eg float,int,
str etc, as I see it, the instances must be of a known class or have a
distinguishing attribute.
As for the timing, when I tried the effect of func1 on our unit tests I
noticed that it slowed the whole test suite by 0.5%. Luckily func 3
style improved things by about 0.3% so that's what I'm going for.
--
Robin Becker
More information about the Python-list
mailing list