vastabc.blogg.se

Power of ten
Power of ten




power of ten

> 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?

power of ten

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






Power of ten