This issue tracker has been migrated to GitHub ,
and is currently read-only.
For more information,
see the GitHub FAQs in the Python's Developer Guide.
| Author | jyasskin |
|---|---|
| Recipients | christian.heimes, gvanrossum, jyasskin, nnorwitz |
| Date | 2008年02月09日.23:02:25 |
| SpamBayes Score | 0.03551593 |
| Marked as misclassified | No |
| Message-id | <1202598148.62.0.43095829707.issue1762@psf.upfronthosting.co.za> |
| In-reply-to |
| Content | |
|---|---|
I measured various implementations of __instancecheck__ using `./python.exe -m timeit -s 'from rational import Rational; r = Rational(3, 2)' '...'` on my 2.33 GHz MacBook, with ... replaced by either isinstance(r, Rational) or isinstance(3, Rational) to measure both the positive and negative cases. The big win comes from avoiding the genexp and the set. Then we win smaller amounts by being more careful about avoiding extra calls to __subclasscheck__ and by inlining the cache checks. # Current code return any(cls.__subclasscheck__(c) for c in set([instance.__class__, type(instance)])) isinstance(3, Rational): 4.65 usec isinstance(r, Rational): 7.47 usec # The best we can do simply in Python return cls.__subclasscheck__(instance.__class__) isinstance(3, Rational): 2.08 usec isinstance(r, Rational): 1.72 usec # Preserve behavior, simply return (cls.__subclasscheck__(instance.__class__) or cls.__subclasscheck__(type(instance))) isinstance(3, Rational): 3.03 usec isinstance(r, Rational): 1.8 usec # Preserve behavior, complexly ic = instance.__class__ if cls.__subclasscheck__(ic): return True t = type(instance) return t is not ic and cls.__subclasscheck__(t) isinstance(3, Rational): 2.38 usec isinstance(r, Rational): 1.86 usec # Inlined for new-style classes subclass = instance.__class__ if subclass in cls._abc_cache: return True type_ = type(instance) if type_ is subclass: if (cls._abc_negative_cache_version == ABCMeta._abc_invalidation_counter and subclass in cls._abc_negative_cache): return False return cls.__subclasscheck__(subclass) return (cls.__subclasscheck__(subclass) or cls.__subclasscheck__(type_)) isinstance(3, Rational): 2.26 usec isinstance(r, Rational): 1.49 usec |
|
| History | |||
|---|---|---|---|
| Date | User | Action | Args |
| 2008年02月09日 23:02:28 | jyasskin | set | spambayes_score: 0.0355159 -> 0.03551593 recipients: + jyasskin, gvanrossum, nnorwitz, christian.heimes |
| 2008年02月09日 23:02:28 | jyasskin | set | spambayes_score: 0.0355159 -> 0.0355159 messageid: <1202598148.62.0.43095829707.issue1762@psf.upfronthosting.co.za> |
| 2008年02月09日 23:02:26 | jyasskin | link | issue1762 messages |
| 2008年02月09日 23:02:25 | jyasskin | create | |