I had a performance issue in a numpy project and then I realized that about 3 fourth of the execution time is wasted on a single line of code:
error = abs(detected_matrix[i, step] - original_matrix[j, new])
and when I have changed the line to
error = abs(original_matrix[j, new] - detected_matrix[i, step])
the problem has disappeared.
I relized that the type of original_matrix
was float64
and type of detected_matrix
was float32
. By changing types of either of these two varibles the problem solved.
I was wondering that if this is a well known issue?
Here is a sample code that represents the problem
from timeit import timeit
import numpy as np
f64 = np.array([1.0], dtype='float64')[0]
f32 = np.array([1.0], dtype='float32')[0]
timeit_result = timeit(stmt="abs(f32 - f64)", number=1000000, globals=globals())
print(timeit_result)
timeit_result = timeit(stmt="abs(f64 - f32)", number=1000000, globals=globals())
print(timeit_result)
Output in my computer:
2.8707289
0.15719420000000017
which is quite strange.