[英]Numba `cache=True ` has no effect
I write below code to test cache feature of numba我写下面的代码来测试 numba 的缓存功能
import numba
import numpy as np
import time
@numba.njit(cache=True)
def sum2d(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
a=np.random.random((1000,100))
print(time.time())
sum2d(a)
print(time.time())
print(time.time())
sum2d(a)
print(time.time())
Though, there are some cache files generated in pycache folder, the timing is always the same like虽然在pycache文件夹中生成了一些缓存文件,但时间总是一样的
1576855294.8787484
1576855295.5378428
1576855295.5378428
1576855295.5388253
no matter how many times I run this script, which means that first run of sum2d
takes much more time to compile.无论我运行这个脚本多少次,这意味着第一次运行sum2d
需要更多的时间来编译。 Then what is usage of cache file in pycache folder?那么pycache文件夹中缓存文件的用途是什么?
The following script illustrates the point of cache=True
.以下脚本说明了cache=True
的要点。 It first calls a non-cached dummy
function that absorbs the time it takes to initialize numba
.它首先调用一个非缓存的dummy
函数来吸收初始化numba
所需的时间。 Then it proceeds with calling twice the sum2d
function with no cache and twice the sum2d
function with cache.然后,它与调用两次进行sum2d
功能,无缓存和两倍的sum2d
与缓存功能。
import numba
import numpy as np
import time
@numba.njit
def dummy():
return None
@numba.njit
def sum2d_nocache(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
@numba.njit(cache=True)
def sum2d_cache(arr):
M, N = arr.shape
result = 0.0
for i in range(M):
for j in range(N):
result += arr[i,j]
return result
start = time.time()
dummy()
end = time.time()
print(f'Dummy timing {end - start}')
a=np.random.random((1000,100))
start = time.time()
sum2d_nocache(a)
end = time.time()
print(f'No cache 1st timing {end - start}')
a=np.random.random((1000,100))
start = time.time()
sum2d_nocache(a)
end = time.time()
print(f'No cache 2nd timing {end - start}')
a=np.random.random((1000,100))
start = time.time()
sum2d_cache(a)
end = time.time()
print(f'Cache 1st timing {end - start}')
a=np.random.random((1000,100))
start = time.time()
sum2d_cache(a)
end = time.time()
print(f'Cache 2nd timing {end - start}')
Output after 1st run:第一次运行后的输出:
Dummy timing 0.10361385345458984
No cache 1st timing 0.08893513679504395
No cache 2nd timing 0.00020122528076171875
Cache 1st timing 0.08929300308227539
Cache 2nd timing 0.00015544891357421875
Output after 2nd run:第二次运行后的输出:
Dummy timing 0.08973526954650879
No cache 1st timing 0.0809786319732666
No cache 2nd timing 0.0001163482666015625
Cache 1st timing 0.0016787052154541016
Cache 2nd timing 0.0001163482666015625
What does this output tells us?这个输出告诉我们什么?
numba
is not negligible.初始化numba
的时间不可忽略。cache=True
is for)在第二次运行中,对缓存函数的第一次调用要快得多(这就是cache=True
的用途) The point of using cache=True
is to avoid repeating the compile time of large and complex functions at each run of a script.使用cache=True
是避免在每次运行脚本时重复大型复杂函数的编译时间。 In this example the function is simple and the time saving is limited but for a script with a number of more complex functions, using cache can significantly reduce the run-time.在这个例子中,函数很简单,节省的时间有限,但对于具有许多更复杂函数的脚本,使用缓存可以显着减少运行时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.