为什么从模块导入的函数比Python中的本地函数明显慢?

发布于 2025-01-17 11:23:17 字数 679 浏览 3 评论 0原文

我正在尝试计算大量数据的标准偏差,首先,我使用统计模块导入该功能。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-01-24 11:23:17

这不是一个答案,

这只是为了更清楚地解释我的一些评论。使用 x = np.random.random(10_000_000) 我得到这些计时:

In [3]: %timeit statistics.pstdev(x)
39.4 s ± 428 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [4]: %timeit get_std_dev(x)
4.53 s ± 132 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [5]: %timeit x.std(ddof=0)
52.6 ms ± 2.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

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:

In [3]: %timeit statistics.pstdev(x)
39.4 s ± 428 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [4]: %timeit get_std_dev(x)
4.53 s ± 132 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

In [5]: %timeit x.std(ddof=0)
52.6 ms ± 2.5 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文