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