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

Optimize floor(), ceil() and trunc() for floats #82820

Closed
serhiy-storchaka opened this issue Oct 30, 2019 · 2 comments
Closed

Optimize floor(), ceil() and trunc() for floats #82820

serhiy-storchaka opened this issue Oct 30, 2019 · 2 comments
Labels
3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage

Comments

@serhiy-storchaka
Copy link
Member

BPO 38639
Nosy @mdickinson, @serhiy-storchaka
PRs
  • bpo-38639: Optimize floor(), ceil() and trunc() for floats. #16991
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-11-16.17:23:10.837>
    created_at = <Date 2019-10-30.09:29:36.791>
    labels = ['interpreter-core', '3.9', 'performance']
    title = 'Optimize floor(), ceil() and trunc() for floats'
    updated_at = <Date 2019-11-16.17:23:10.837>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2019-11-16.17:23:10.837>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-11-16.17:23:10.837>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2019-10-30.09:29:36.791>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38639
    keywords = ['patch']
    message_count = 2.0
    messages = ['355699', '356753']
    nosy_count = 2.0
    nosy_names = ['mark.dickinson', 'serhiy.storchaka']
    pr_nums = ['16991']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'performance'
    url = 'https://bugs.python.org/issue38639'
    versions = ['Python 3.9']

    @serhiy-storchaka
    Copy link
    Member Author

    Currently math.floor(), math.ceil() and math.trunc() look up special methods __floor__, __ceil__ and __trunc__ correspondingly and execute them if found. Otherwise they execute common code for floats. There are no special slots for these methods, so looking up these names in type dicts takes some time. It is a waste of time for most common case -- float objects.

    The proposed PR adds checks PyFloat_CheckExact() before looking up the special method. Some microbenchmarks:

    $ ./python -m pyperf timeit -s "from math import floor" --duplicate 100 "floor(12345.6)"
    Before:  Mean +- std dev: 63.2 ns +- 1.7 ns
    After:   Mean +- std dev: 51.8 ns +- 1.3 ns
    
    $ ./python -m pyperf timeit -s "from math import ceil" --duplicate 100 "ceil(12345.6)"
    Before:  Mean +- std dev: 61.1 ns +- 1.5 ns
    After:   Mean +- std dev: 51.9 ns +- 1.1 ns
    
    $ ./python -m pyperf timeit -s "from math import trunc" --duplicate 100 "trunc(12345.6)"
    Before:  Mean +- std dev: 72.0 ns +- 1.5 ns
    After:   Mean +- std dev: 34.7 ns +- 1.4 ns

    We should also check how this optimization affects other types:

    $ ./python -m pyperf timeit -s "from math import floor" --duplicate 100 "floor(12345)"
    Before:  Mean +- std dev: 56.3 ns +- 1.3 ns
    After:   Mean +- std dev: 56.3 ns +- 1.4 ns
    
    $ ./python -m pyperf timeit -s "from math import ceil" --duplicate 100 "ceil(12345)"
    Before:  Mean +- std dev: 55.7 ns +- 1.6 ns
    After:   Mean +- std dev: 56.8 ns +- 1.6 ns
    
    $ ./python -m pyperf timeit -s "from math import trunc" --duplicate 100 "trunc(12345)"
    Before:  Mean +- std dev: 54.7 ns +- 1.3 ns
    After:   Mean +- std dev: 56.7 ns +- 1.5 ns

    @serhiy-storchaka serhiy-storchaka added 3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage labels Oct 30, 2019
    @serhiy-storchaka serhiy-storchaka changed the title Optimize floor() and ceil() for floats Optimize floor(), ceil() and trunc() for floats Oct 30, 2019
    @serhiy-storchaka
    Copy link
    Member Author

    New changeset 5fd5cb8 by Serhiy Storchaka in branch 'master':
    bpo-38639: Optimize floor(), ceil() and trunc() for floats. (GH-16991)
    5fd5cb8

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) performance Performance or resource usage
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant