[New-bugs-announce] [issue2534] Speed up isinstance and issubclass

Thomas Heller report at bugs.python.org
Wed Apr 2 11:47:48 CEST 2008


New submission from Thomas Heller <theller at ctypes.org>:

This patch implements type.__instancecheck__ and type.__subclasscheck__,
which speeds up isinstance and issubclass calls quite a bit.

See also issue #2303.

Here are the performance figures for the current trunk version:

Current SNV trunk:

Using 2.6a1+ (trunk:62102, Apr  2 2008, 11:30:16) [MSC v.1500 32 bit
(Intel)]
isinstance(42, int)	   1000000 loops, best of 3: 0.28 usec per loop
isinstance(42, type)	   1000000 loops, best of 3: 0.974 usec per loop
issubclass(object, type)   1000000 loops, best of 3: 1.1 usec per loop
issubclass(object, int)    1000000 loops, best of 3: 1.1 usec per loop
issubclass(float, int)     1000000 loops, best of 3: 1.15 usec per loop

Current trunk, patch applied:

Using 2.6a1+ (trunk:62102M, Apr  2 2008, 11:21:32) [MSC v.1500 32 bit
(Intel)]
isinstance(42, int)	   1000000 loops, best of 3: 0.274 usec per loop
isinstance(42, type)	   1000000 loops, best of 3: 0.524 usec per loop
issubclass(object, type)   1000000 loops, best of 3: 0.661 usec per loop
issubclass(object, int)    1000000 loops, best of 3: 0.662 usec per loop
issubclass(float, int)     1000000 loops, best of 3: 0.731 usec per loop

Python 2.5.2, for comparison:

Using 2.5.2 (r252:60911, Feb 21 2008, 13:11:45) [MSC v.1310 32 bit (Intel)]
isinstance(42, int)	   1000000 loops, best of 3: 0.292 usec per loop
isinstance(42, type)	   1000000 loops, best of 3: 0.417 usec per loop
issubclass(object, type)   1000000 loops, best of 3: 0.626 usec per loop
issubclass(object, int)    1000000 loops, best of 3: 0.648 usec per loop
issubclass(float, int)     1000000 loops, best of 3: 0.752 usec per loop

----------
files: type_instancecheck.diff
keywords: patch, patch
messages: 64842
nosy: theller
severity: normal
status: open
title: Speed up isinstance and issubclass
type: performance
versions: Python 2.6
Added file: http://bugs.python.org/file9924/type_instancecheck.diff

__________________________________
Tracker <report at bugs.python.org>
<http://bugs.python.org/issue2534>
__________________________________


More information about the New-bugs-announce mailing list