![]() So the fastest of these options in both cases is the obvious one: 10 ** n for integers and 10.0 ** n for floats.Īnother contender for the floats case, precompute all possible nonzero finite results and look them up: 0.0 if n < -323 else f if n < 309 else inf ![]() > timeit('f(n)', setup='n = 200 from functools import partial import numpy as np f = partial(np.power, 10.0, dtype=float)') > timeit('np.power(10.0, n, dtype=float)', setup='n = 200 import numpy as np') 10 ** n computes an integer (when n is non-negative), whereas float(f'1e")', setup='n = 200') So is the f-string actually the best way to go about it? I guess the f-string approach is the fastest because nothing is actually calculated, though it only works for integer powers of ten, whereas the other methods are more complicated operations that also work with any real number as the base and power. # ValueError: Integers to negative integer powers are not allowed. # "dtype=float" is necessary because otherwise it will raise: ![]() 'np.pow': partial(np.power, 10, dtype=float) If as the input you provide the (integer) power, what is the fastest way to create the corresponding power of ten? Here are four alternatives I could come up with, and the fastest way seems to be using an f-string: from functools import partial ![]()
0 Comments
Leave a Reply. |