Closed ProgrammerPeter closed 4 months ago
The speed improvement comes not from vectorization, but from the precision of calculations. Initially, it is calculated in float64
.
import timeit
import numpy as np
pixel_values = np.random.rand(256, 256, 3)
def pix_val_1():
return (pixel_values / 127.5 - 1.0).astype(np.float32)
def pix_val_2():
return pixel_values.astype(np.float32) / 127.5 - 1.0
def pix_val_3():
return np.subtract(np.divide(pixel_values, 127.5, dtype=np.float32), 1.0, dtype=np.float32)
n = 50000
print(timeit.timeit(pix_val_1, number=n) / n)
print(timeit.timeit(pix_val_2, number=n) / n)
print(timeit.timeit(pix_val_3, number=n) / n)
Output:
0.0008699686280000605
0.0002086409739998635
0.00022953715799987548
The speed improvement comes not from vectorization, but from the precision of calculations. Initially, it is calculated in
float64
.import timeit import numpy as np pixel_values = np.random.rand(256, 256, 3) def pix_val_1(): return (pixel_values / 127.5 - 1.0).astype(np.float32) def pix_val_2(): return pixel_values.astype(np.float32) / 127.5 - 1.0 def pix_val_3(): return np.subtract(np.divide(pixel_values, 127.5, dtype=np.float32), 1.0, dtype=np.float32) n = 50000 print(timeit.timeit(pix_val_1, number=n) / n) print(timeit.timeit(pix_val_2, number=n) / n) print(timeit.timeit(pix_val_3, number=n) / n)
Output:
0.0008699686280000605 0.0002086409739998635 0.00022953715799987548
nice finding
What does this PR do?
Improve performance of data preprocess.
Fixes # (issue) Import normalization performance by vectorization.
Before:
After:
Adds # (feature) Nothing.
Before submitting
What's New
. Here are the documentation guidelinesWho can review?
Anyone in the community is free to review the PR once the tests have passed. Feel free to tag members/contributors who may be interested in your PR.
@xxx