简体   繁体   English

Numba `cache=True` 没有效果

[英]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?这个输出告诉我们什么?

  • The time to initialize numba is not negligible.初始化numba的时间不可忽略。
  • During the first run, the first call of the cache and non-cache version take longer due to compilation time.在第一次运行期间,由于编译时间,缓存和非缓存版本的第一次调用需要更长的时间。
  • In this example, the creation of the cache file doesn't make much of a difference.在此示例中,缓存文件的创建没有太大区别。
  • In the second run, the first call to the cache function is much faster (this is what cache=True is for)在第二次运行中,对缓存函数的第一次调用要快得多(这就是cache=True的用途)
  • The subsequent calls to the cache and non-cache functions take approximately the same time.对缓存和非缓存函数的后续调用花费的时间大致相同。

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.

相关问题 Numba 尝试:如果 array.shape[1] - 错误:元组索引超出范围。 没有 numba 的情况下工作,不适用于 @njit(fastmath=True, nogil=True, cache=True) - Numba try: if array.shape[1] - error: tuple index out of range. works without numba, doesn't work with @njit(fastmath=True, nogil=True, cache=True) Numba:什么时候使用 nopython=True? - Numba: when to use nopython=True? "<i>Numba bytecode generation for generic x64 processors?<\/i>通用 x64 处理器的 Numba 字节码生成?<\/b> <i>Rather than 1st run compiling a SLOW @njit(cache=True) argument<\/i>而不是第一次运行编译 SLOW @njit(cache=True) 参数<\/b>" - Numba bytecode generation for generic x64 processors? Rather than 1st run compiling a SLOW @njit(cache=True) argument 在 ForeignKey 上设置 unique=True 与使用 OneToOneField 具有相同的效果 - Setting unique=True on a ForeignKey has the same effect as using a OneToOneField 为什么使用这种可并行化的 for 循环,带有 parallel=True 的 numba 会更慢? - Why numba with parallel=True is slower with this parallelizable for loop? Numba Jit 自动缓存与磁盘缓存 - Numba Jit auto cache vs on disk caching Numba parallel = True 比 parallel = False 慢 - Numba parallel = True is slower than with parallel = False Keras Transfer-Learning 设置 layers.trainable 为 True 没有效果 - Keras Transfer-Learning setting layers.trainable to True has no effect 为什么numba在numpy方法时抛出错误(nopython = True)? - Why is numba throwing an error regarding numpy methods when (nopython=True)? numba @jit(nopython=True):使用字典作为值的重构函数 - numba @jit(nopython=True): Refactor function that uses dictionary with lists as values
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM