首页 > 解决方案 > `numpy.argmax()` 的理论平均案例运行时复杂度

问题描述

我在看numpy.argmax函数的代码。我很困惑该函数numpy维护哪个数据结构。argmax

https://numpy.org/doc/stable/reference/generated/numpy.argmax.html

最后,我想知道原始数据类型的函数的理论平均情况运行时间复杂度是多少。numpy argmax是这样O(logN)还是O(N)一般情况下?

这也可能是一个相关问题:Faster alternatives to numpy.argmax/argmin which is slow

提前致谢。

标签: pythonnumpydata-structuresmax

解决方案


这是使用的性能分析benchit

def m(x):
  return np.argmax(x)

in_ = [np.random.rand(n) for n in [10,100,1000,10000]]

正如你所看到的O(N),它应该是这样的。您遍历数组一次以找到最大值。

在此处输入图像描述


推荐阅读