This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author rhettinger
Recipients rhettinger
Date 2021-12-09.06:32:23
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1639031543.32.0.0830001202943.issue46020@roundup.psfhosted.org>
In-reply-to
Content
The expression 'x * x' is faster than 'x ** 2'.

In Python3.10, the speed difference was enormous.  Due to ceval optimizations, the difference in Python3.11 is tighter; however, there is still room for improvement.

The code for long_pow() doesn't currently have a fast path for squaring which is by far the most important case.

$ python3.10 -m timeit -r 11 -s 'x = 10' 'x ** 2'
1000000 loops, best of 11: 201 nsec per loop
$ python3.10 -m timeit -r 11 -s 'x = 10' 'x * x'
10000000 loops, best of 11: 21.9 nsec per loop

$ python3.11 -m timeit -r 11 -s 'x = 10' 'x ** 2'
10000000 loops, best of 11: 32 nsec per loop
$ python3.11 -m timeit -r 11 -s 'x = 10' 'x * x'
20000000 loops, best of 11: 17.6 nsec per loop
History
Date User Action Args
2021-12-09 06:32:23rhettingersetrecipients: + rhettinger
2021-12-09 06:32:23rhettingersetmessageid: <1639031543.32.0.0830001202943.issue46020@roundup.psfhosted.org>
2021-12-09 06:32:23rhettingerlinkissue46020 messages
2021-12-09 06:32:23rhettingercreate