-
- Notifications
You must be signed in to change notification settings - Fork 19.4k
Closed
Description
Observe:
In [62]: %timeit com.is_integer(13) 1000000 loops, best of 3: 965 ns per loop In [63]: %timeit com.is_integer(np.int32(2)) 100000 loops, best of 3: 2.56 µs per loop In [64]: %timeit com.is_integer(np.int64(2)) 1000000 loops, best of 3: 1.27 µs per loop In [65]: %timeit com.is_integer(14L) 1000000 loops, best of 3: 970 ns per loop In [76]: %timeit com.is_integer("1") 1000000 loops, best of 3: 1.66 µs per loop The lower bound of this function would be:
In [66]: %timeit type(3) is int 10000000 loops, best of 3: 85.8 ns per loop My first attempt is the following which pulls the tuple creation out and optimizes for the common case of calling with int. This seems to make all cases faster, especially the common, int, case:
_int_types = (int, numbers.Integral, np.integer) def is_integer(obj): return isinstance(obj, _int_types) and results:
In [70]: %timeit is_integer(13) 10000000 loops, best of 3: 146 ns per loop In [71]: %timeit is_integer(np.int32(2)) 100000 loops, best of 3: 2.43 µs per loop In [72]: %timeit is_integer(np.int64(2)) 1000000 loops, best of 3: 375 ns per loop In [73]: %timeit is_integer(14L) 1000000 loops, best of 3: 911 ns per loop In [75]: %timeit is_integer("1") 1000000 loops, best of 3: 1.57 µs per loop Metadata
Metadata
Assignees
Labels
No labels