Skip to content

PERF/CLN: float-to-int casting #31409

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

Merged
merged 5 commits into from
Feb 2, 2020
Merged

Conversation

jbrockmendel
Copy link
Member

using float.is_integer is apparently much faster than casting and checking equality, especially for values between int32max and int64max (anecdotal based on small sample size)

cast1 = com.cast_scalar_indexer_master
cast2 = com.cast_scalar_indexer_PR

def cast3(key):
    # Effectively what we have in _maybe_cast_indexer in master
    if lib.is_float(key):
        try:
            ckey = int(key)
            if ckey == key:
                key = ckey
        except (OverflowError, ValueError, TypeError):
            pass
    return key

key1 = np.float64(17179869184.0)
key2 = float(key1) * 2**30             
key3 = np.float64(np.iinfo(np.int64).max)

In [87]: for func in [cast1, cast2, cast3]: 
    ...:     for key in [key1, key2, key3]: 
    ...:                 %timeit func(key) 
    ...:                                                                                                                                                                                                                                          

647 ns ± 3.71 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
559 ns ± 27.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.98 µs ± 19.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

360 ns ± 4.1 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
353 ns ± 3.11 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
377 ns ± 6.54 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

568 ns ± 3.92 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
445 ns ± 15 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)
1.91 µs ± 36.7 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

The middle three timeit results are the ones we get from this PR.

@jorisvandenbossche
Copy link
Member

There should be a similar case in arrays/integer.py that you can update, I think

@jreback jreback added Dtype Conversions Unexpected or buggy dtype conversions Performance Memory or execution speed performance labels Jan 31, 2020
@jreback
Copy link
Contributor

jreback commented Jan 31, 2020

more cases in pandas/core/dtypes/cast.py no?

Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also cast_scalar_indexer really belongs in pandas/core/dtypes/cast.py as well (common.py is a hodge podge)

@jbrockmendel
Copy link
Member Author

more cases in pandas/core/dtypes/cast.py no?

Just did a pass through the file, didnt find any.

also cast_scalar_indexer really belongs in pandas/core/dtypes/cast.py as well (common.py is a hodge podge)

Probably. I've got a lot of refactor-esque PRs/branches going, will move this after the current batch.

@jreback jreback added this to the 1.1 milestone Feb 1, 2020
@jreback
Copy link
Contributor

jreback commented Feb 1, 2020

lgtm. needs a rebase.

@jbrockmendel
Copy link
Member Author

rebased+green

@jreback jreback merged commit f878ddc into pandas-dev:master Feb 2, 2020
@jreback
Copy link
Contributor

jreback commented Feb 2, 2020

thanks

@jbrockmendel jbrockmendel deleted the cast-float branch February 2, 2020 23:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Dtype Conversions Unexpected or buggy dtype conversions Performance Memory or execution speed performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants