Old version (total time for 10 iterations): real 0m1.532s user 0m1.533s sys 0m0.000s New version (total time for 10 iterations): real 6m48.716s user 6m49.163s sys 0m0.000s Optimized new version (total time for 10 iterations): real 5m20.007s user 5m20.357s sys 0m0.000s