为什么从模块导入的函数比Python中的本地函数明显慢?
我正在尝试计算大量数据的标准偏差,首先,我使用统计模块导入该功能。
from statistics import pstdev
但是结果非常慢,所以我决定编写一种当地的助手方法,该方法做得完全相同,
def get_std_dev(ls):
n = len(ls)
mean = sum(ls) / n
var = sum((x - mean)**2 for x in ls) / n
std_dev = var ** 0.5
return std_dev
这要快得多!这是运行时比较,
Runtime with my written function: 0:00:00.532228
Runtime with imported module function: 0:00:17.605583
我非常困惑,为什么与本地书面功能相比,导入功能如此慢。它与内存位置有关吗?
这两个函数之间的唯一区别是这些代码
stdev = get_std_dev(close_price_list) # my written one
stdev = pstdev(close_price_list) # the imported function
I'm trying to calculate the standard deviation for a large set of data, at first I used the statistics module to import the function.
from statistics import pstdev
But the result is very slow, so I decided to write a local helper method which does the exactly same thing,
def get_std_dev(ls):
n = len(ls)
mean = sum(ls) / n
var = sum((x - mean)**2 for x in ls) / n
std_dev = var ** 0.5
return std_dev
This runs significantly faster! Here are the runtime comparison
Runtime with my written function: 0:00:00.532228
Runtime with imported module function: 0:00:17.605583
I am very confused why the imported function is so slow compared to my local written function. Does it have to do with memory location?
The only difference between the two functions are the these pieces of codes
stdev = get_std_dev(close_price_list) # my written one
stdev = pstdev(close_price_list) # the imported function
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是一个答案,
这只是为了更清楚地解释我的一些评论。使用 x = np.random.random(10_000_000) 我得到这些计时:
This is not an answer
This is just to explain some of my comments more clearly. With
x = np.random.random(10_000_000)
I get these timings: