Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

com.is_integer is slow #6264

Closed
cancan101 opened this issue Feb 5, 2014 · 1 comment · Fixed by #6269
Closed

com.is_integer is slow #6264

cancan101 opened this issue Feb 5, 2014 · 1 comment · Fixed by #6269

Comments

@cancan101
Copy link
Contributor

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
@jreback
Copy link
Contributor

jreback commented Feb 5, 2014

@cancan101 already had #6266 queued up.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants