GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Avoid repetitive calculations of "10 ** x" for the most common 0 <= x… · scoder/quicktions@c20add5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • src
    • quicktions.pyx
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/quicktions.pyx‎

Copy file name to clipboardExpand all lines: src/quicktions.pyx
+31-5Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,30 @@ import sys
4343
cdef bint _decimal_supports_integer_ratio = hasattr(Decimal, "as_integer_ratio") # Py3.6+
4444

4545

46+
# Cache widely used 10**x int objects.
47+
DEF CACHED_POW10 = 64
48+
49+
cdef tuple _cache_pow10():
50+
cdef int i
51+
l = []
52+
x = 1
53+
for i in range(CACHED_POW10):
54+
l.append(x)
55+
x *= 10
56+
return tuple(l)
57+
58+
cdef tuple POW_10 = _cache_pow10()
59+
60+
61+
cdef pow10(Py_ssize_t i):
62+
if 0 <= i < CACHED_POW10:
63+
return POW_10[i]
64+
else:
65+
return 10 ** (<object> i)
66+
67+
68+
# Half-private GCD implementation.
69+
4670
cdef unsigned long long _abs(long long x):
4771
if x == PY_LLONG_MIN:
4872
return (<unsigned long long>PY_LLONG_MAX) + 1
@@ -244,6 +268,7 @@ cdef class Fraction:
244268
@classmethod
245269
def from_decimal(cls, dec):
246270
"""Converts a finite Decimal instance to a rational number, exactly."""
271+
cdef Py_ssize_t exp
247272
if isinstance(dec, numbers.Integral):
248273
dec = Decimal(int(dec))
249274
elif not isinstance(dec, Decimal):
@@ -258,9 +283,9 @@ cdef class Fraction:
258283
if sign:
259284
digits = -digits
260285
if exp >= 0:
261-
return cls(digits * 10 ** exp)
286+
return cls(digits * pow10(exp))
262287
else:
263-
return cls(digits, 10 ** -exp)
288+
return cls(digits, pow10(-exp))
264289

265290
def limit_denominator(self, max_denominator=1000000):
266291
"""Closest Fraction to self with denominator at most max_denominator.
@@ -458,7 +483,7 @@ cdef class Fraction:
458483
return floor
459484
else:
460485
return floor + 1
461-
shift = 10**abs(ndigits)
486+
shift = pow10(abs(<Py_ssize_t>ndigits))
462487
# See _operator_fallbacks.forward to check that the results of
463488
# these operations will always be Fraction and therefore have
464489
# round().
@@ -822,6 +847,7 @@ cdef _raise_invalid_input(s):
822847

823848
cdef tuple _parse_fraction(AnyString s):
824849
cdef size_t i, dec_pos = 0, exp_pos = 0, dec_len = 0
850+
cdef Py_ssize_t shift
825851
cdef Py_UCS4 c
826852
cdef AnyString numerator, denominator
827853

@@ -904,8 +930,8 @@ cdef tuple _parse_fraction(AnyString s):
904930
if neg:
905931
num = -num
906932
if shift > 0:
907-
num *= 10 ** shift
933+
num *= pow10(shift)
908934
elif shift < 0:
909-
denom = 10 ** -shift
935+
denom = pow10(-shift)
910936

911937
return num, denom

0 commit comments

Comments
0 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.