I am aware that there exist several libraries and programs that allow to baktest a portfolio strategy by iterating through the OHLC dataframe of the stocks of interest (Backtrader, Backtesting, ...). However, these methods are useful when we are working on one or few stocks: when it comes to backtest a strategy that includes several stocks (thousands), it becomes impossible to iterate though all these datasets.
In these cases, how can we determine the ex-post returns of a portfolio containing such quantity of instruments? For example, if we know the weights for each stock at each trading day to invest in the portfolio, how can we test such strategy?