Python中对数计算的底数会影响速度吗?

Does the base for logarithmic calculations in Python influence the speed?

我不得不在一个程序中使用大量的对数计算。就对数底而言,该过程不具体。我想知道,如果 n (2? 10? e?) 在 Python 3.5 math 模块中比其他模块更快,因为可能在幕后所有其他基地 a 被转化为 log_a(x) = log_n(x)/log_n(a)。或者基数的选择不会影响计算速度,因为所有基数都是使用 C 库以相同的方式实现的?

有趣的问题。我做了一些 "good old" 现场测试(CPython 3.6.2 on Linux, x86_64, i7-3740QM CPU - Python 解释器编译此 CPU 可用的所有优化已打开)。

>>> 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 明显比 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 也明显比 log(n, 2) 快。顺便说一句,无论哪种方式,浮点数和整数都同样快。

有了numpy,画面就不一样了。你做什么并不重要:

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

在 CPython 中,math.log 独立于基础,但依赖于平台。从 C source for the math module 开始,第 1940-1961 行显示了 math.log 的代码。

math_log_impl(PyObject *module, PyObject *x, int group_right_1,
          PyObject *base)
/*[clinic end generated code: output=7b5a39e526b73fc9 input=0f62d5726cbfebbd]*/

{
    PyObject *num, *den;
    PyObject *ans;

    num = loghelper(x, m_log, "log"); // uses stdlib log
    if (num == NULL || base == NULL)
        return num;

    den = loghelper(base, m_log, "log"); // uses stdlib log
    if (den == NULL) {
        Py_DECREF(num);
        return NULL;
    }

    ans = PyNumber_TrueDivide(num, den);
    Py_DECREF(num);
    Py_DECREF(den);
    return ans;
}

无论如何,这都会计算数字和基数的自然对数,所以除非 C log 函数对 e 有特殊检查,否则它将 运行 在同样的速度。

此来源还解释了其他答案的 log2log10log 更快。它们分别使用标准库log2log10函数实现,速度会更快。然而,这些函数的定义因平台而异。

注意:我对C不是很熟悉所以这里可能是错误的。