Well I actually see no other way to go around the for loop coz a recursive approach will even take more time and memory what I suggest is that u try to run this process in parallel on the processor u are using or run it on a gpu if available (u can use a gpu computing cloud service)