Skip to content

com.is_integer is slow #6264

Closed
Closed
@cancan101

Description

@cancan101

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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions