In python, which one is faster ?
在python中,哪一个更快?
numpy.max(), numpy.min()
or
max(), min()
My list/array length varies from 2 to 600. Which one should I use to save some run time ?
我的列表/数组长度从2到600不等。我应该使用哪一个来节省一些运行时间?
4 个解决方案
#1
39
Well from my timings it follows if you already have numpy array a
you should use a.max
(the source tells it's the same as np.max
if a.max
available). But if you have built-in list then most of the time takes converting it into np.ndarray => that's why max
is better in your timings.
从我的时间开始,如果你已经有numpy数组,你应该使用a.max(如果a.max可用,则来源告诉它与np.max相同)。但是如果你有内置列表,那么大部分时间都会把它转换成np.ndarray =>这就是为什么max在你的时间更好。
In essense: if np.ndarray
then a.max
, if list
and no need for all the machinery of np.ndarray
then standard max
.
在本能:如果np.ndarray然后a.max,如果列表并且不需要np.ndarray的所有机器那么标准最大。
#2
19
I was also interested in this and tested the three variants with perfplot (a little project of mine). Result: You're not going wrong with a.max()
.
我也对此感兴趣,并使用perfplot(我的一个小项目)测试了这三个变体。结果:a.max()你不会出错。
Code to reproduce the plot:
重现情节的代码:
import numpy
import perfplot
perfplot.show(
setup=lambda n: numpy.random.rand(n),
kernels=[
max,
numpy.max,
lambda a: a.max()
],
labels=['max(a)', 'numpy.max(a)', 'a.max()'],
n_range=[2**k for k in range(25)],
logx=True,
logy=True,
xlabel='len(a)'
)
#3
9
It's probably best if you use something like the Python timeit module to test it for yourself. That way you can test your own data in your own environment, rather than relying on third parties with various test data and environments which aren't necessarily representative of yours.
如果您使用类似Python timeit模块的东西来自己测试它,那可能是最好的。这样,您就可以在自己的环境中测试自己的数据,而不是依赖于具有各种测试数据和环境的第三方,这些数据和环境不一定代表您的。
#4
2
numpy.min
and numpy.max
have slightly different semantics (and call signatures) to the builtins, so the choice shouldn't be to do with speed. Use the numpy versions if you need to be able to handle multidimensional data sanely. If you're just using Python lists or other things that don't know about dimensionality, use the builtins.
numpy.min和numpy.max对内置函数的语义(和调用签名)略有不同,因此选择不应该与速度有关。如果您需要能够理智地处理多维数据,请使用numpy版本。如果您只是使用Python列表或其他不了解维度的东西,请使用builtins。
#1
39
Well from my timings it follows if you already have numpy array a
you should use a.max
(the source tells it's the same as np.max
if a.max
available). But if you have built-in list then most of the time takes converting it into np.ndarray => that's why max
is better in your timings.
从我的时间开始,如果你已经有numpy数组,你应该使用a.max(如果a.max可用,则来源告诉它与np.max相同)。但是如果你有内置列表,那么大部分时间都会把它转换成np.ndarray =>这就是为什么max在你的时间更好。
In essense: if np.ndarray
then a.max
, if list
and no need for all the machinery of np.ndarray
then standard max
.
在本能:如果np.ndarray然后a.max,如果列表并且不需要np.ndarray的所有机器那么标准最大。
#2
19
I was also interested in this and tested the three variants with perfplot (a little project of mine). Result: You're not going wrong with a.max()
.
我也对此感兴趣,并使用perfplot(我的一个小项目)测试了这三个变体。结果:a.max()你不会出错。
Code to reproduce the plot:
重现情节的代码:
import numpy
import perfplot
perfplot.show(
setup=lambda n: numpy.random.rand(n),
kernels=[
max,
numpy.max,
lambda a: a.max()
],
labels=['max(a)', 'numpy.max(a)', 'a.max()'],
n_range=[2**k for k in range(25)],
logx=True,
logy=True,
xlabel='len(a)'
)
#3
9
It's probably best if you use something like the Python timeit module to test it for yourself. That way you can test your own data in your own environment, rather than relying on third parties with various test data and environments which aren't necessarily representative of yours.
如果您使用类似Python timeit模块的东西来自己测试它,那可能是最好的。这样,您就可以在自己的环境中测试自己的数据,而不是依赖于具有各种测试数据和环境的第三方,这些数据和环境不一定代表您的。
#4
2
numpy.min
and numpy.max
have slightly different semantics (and call signatures) to the builtins, so the choice shouldn't be to do with speed. Use the numpy versions if you need to be able to handle multidimensional data sanely. If you're just using Python lists or other things that don't know about dimensionality, use the builtins.
numpy.min和numpy.max对内置函数的语义(和调用签名)略有不同,因此选择不应该与速度有关。如果您需要能够理智地处理多维数据,请使用numpy版本。如果您只是使用Python列表或其他不了解维度的东西,请使用builtins。