I’m writing a program that needs to do a lot of string formatting and I have noticed that .format() is taking a small but significant amount of cpu time. Here’s how I’m using it:
str = 'vn {0:.15f} {1:.15f} {2:.15f}\n'.format(normal_array[i].x, normal_array[i].y, normal_array[i].z)
Does anyone know if there is even a slightly faster way to do this as a small fraction X 100000 can add up
Try to replace
.formatwith%expression and pre-calculate normal_array:Also replacing indexes with iteration over values can slightly improve speed:
Benchmark:
Results (lower is better)