Home > Software design >  Python for loop takes over hours, how to reduce execution time?
Python for loop takes over hours, how to reduce execution time?

Time:05-06

I'm using a for loop to get a particular financial data (around 800 sets) from yfinance. But the running time for this execution is over an hour! This is just a small part of my whole project. How to reduce the execution time?

S&P500


import yfinance as yf

# symbols = [x for x in tqdm(symbol['Symbol'])]

symbols = ['AAPL', 'MSFT'] # Testing only, Swap for yours

tickers = yf.Tickers(' '.join(symbols))

institutional_holders = {x:tickers.tickers[x].institutional_holders.head() for x in symbols}

df = pd.concat(institutional_holders)

print(df)

Output:

                               Holder      Shares Date Reported   % Out         Value
AAPL 0     Vanguard Group, Inc. (The)  1261261357    2021-12-30  0.0779  223962179162
     1                 Blackrock Inc.  1019810291    2021-12-30  0.0630  181087713372
     2        Berkshire Hathaway, Inc   887135554    2021-12-30  0.0548  157528660323
     3       State Street Corporation   633115246    2021-12-30  0.0391  112422274232
     4                       FMR, LLC   352204129    2021-12-30  0.0218   62540887186
MSFT 0     Vanguard Group, Inc. (The)   615950062    2021-12-30  0.0824  207156324851
     1                 Blackrock Inc.   519035634    2021-12-30  0.0694  174562064426
     2       State Street Corporation   302541869    2021-12-30  0.0405  101750881382
     3                       FMR, LLC   215377233    2021-12-30  0.0288   72435671002
     4  Price (T.Rowe) Associates Inc   204196901    2021-12-30  0.0273   68675501744
  • Related