in reply to Re: Variable number of foreach loops (improving performance)in thread Variable number of foreach loops
You'll gain a lot more speed rewriting it in XS and using the multicall interface to call $code.