Interest Ask. I did a "good old" field test (CPython 3.6.2 on Linux, x86_64, i7-3740QM CPU - the Python interpreter compiled with all the optimizations available for this CPU is included).
>>> math.log10(3) 0.47712125471966244 >>> math.log(3, 10) 0.47712125471966244 >>> timeit.timeit('math.log(3, 10)', setup = 'import math') 0.2496643289923668 >>> timeit.timeit('math.log10(3)', setup = 'import math') 0.14756392200069968
Log10 is clearly faster than log (n, 10).
>>> math.log2(3.0) 1.584962500721156 >>> math.log(3.0, 2.0) 1.5849625007211563 >>> timeit.timeit('math.log2(3.0)', setup = 'import math') 0.16744944200036116 >>> timeit.timeit('math.log(3.0, 2.0)', setup = 'import math') 0.22228705599263776
Log2 is also clearly faster than log (n, 2). Btw, in any case, floats and ints are equally fast.
With numpy image is different. It doesnβt matter what you do:
>>> timeit.timeit('numpy.log(numpy.arange(1, 10))', setup = 'import numpy') 2.725074506000965 >>> timeit.timeit('numpy.log10(numpy.arange(1, 10))', setup = 'import numpy') 2.613872367001022 >>> timeit.timeit('numpy.log2(numpy.arange(1, 10))', setup = 'import numpy') 2.58251854799164
source share