Why importing the result of a procedure is faster than running the procedure in Python?

Viewed 36

It looks like running a procedure in a separate file and importing the result variable (or database) is faster than running the exact same procedure in the main code file. Is that always the case in Python? And why is that? Appreciate your thoughts on that

I am using an example with YahooFinancials, which is a relative heavy application, but this simulation works with pretty much any heavy procedure.

Original Code :

import time

st = time.time()

from yahoofinancials import YahooFinancials

tickers = ['ESPO', 'SPY', 'JXI', 'EUR=X', 'CAD=X', 'CHF=X', 'ETH-USD'] 
freq = 'daily'
start_date = '2022-09-12'
end_date = start_date

tic = YahooFinancials(tickers)
hist = tic.get_historical_price_data(start_date, end_date, freq)

et=time.time()

print(f'Total time spent : {round(et - st,2)} seconds')
>>Total time spent : 16.77 seconds

If I run the exact same thing on a different file anotherFile.py and import the hist variable into the main file, the time executed is much smaller :

import time

st = time.time()

from anotherFile import hist

et=time.time()

print(f'Total time spent : {round(et - st,2)} seconds')
>>Total time spent : 1.76 seconds

Of course, using a big list of tickers or a heavier procedure it gets much slower.

Thank you very much

0 Answers
Related