如何在numba.jit函数中测量时间? [英] How to measure time in numba.jit function?

查看:124
本文介绍了如何在numba.jit函数中测量时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将常规循环转换为 numba.jit 函数,并在内部测量其处理时间.我尝试使用 time 模块,但它似乎与numba不兼容.

I want to convert a conventional loop into a numba.jit function and measure time of its processes internally. I tried using time module but it doesn't seem to be compatible with numba.

代码:

from numba import jit, jitclass
import time

@jit(nopython=True)
def harmonic_load_flow_func():
    time1 = time.perf_counter()
    calc = 0
    for x in range(1000000):
        calc += x
    print('time: {}'.format(time.perf_counter() - time1))

if __name__ == '__main__':
    for count in range(10):
        harmonic_load_flow_func()

输出:

C:\Users\Artur\Anaconda\python.exe C:/Users/Artur/Desktop/RL_framework/help_functions/test.py
Traceback (most recent call last):
  File "C:/Users/Artur/Desktop/RL_framework/help_functions/test.py", line 14, in <module>
    harmonic_load_flow_func()
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 401, in _compile_for_args
    error_rewrite(e, 'typing')
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\dispatcher.py", line 344, in error_rewrite
    reraise(type(e), e, None)
  File "C:\Users\Artur\Anaconda\lib\site-packages\numba\core\utils.py", line 80, in reraise
    raise value.with_traceback(tb)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Unknown attribute 'perf_counter' of type Module(<module 'time' (built-in)>)

File "test.py", line 6:
def harmonic_load_flow_func():
    time1 = time.perf_counter()
    ^

[1] During: typing of get attribute at C:/Users/Artur/Desktop/RL_framework/help_functions/test.py (6)

File "test.py", line 6:
def harmonic_load_flow_func():
    time1 = time.perf_counter()
    ^


Process finished with exit code 1

推荐答案

是的,它不支持时间模块.

Yeah, it does not support the time module.

您可以使用 objmode()或使用简单的python函数包装整个调用并测量:

You can either use objmode() or wrap the whole call with plain python function and measure:

from numba import jit, objmode
import time

@jit(nopython=True)
def harmonic_load_flow_func_time_inside():
    with objmode(time1='f8'):
        time1 = time.perf_counter()
    calc = 0
    for x in range(1000000):
        calc += x
    with objmode():
        print('time: {}'.format(time.perf_counter() - time1))

@jit(nopython=True)
def harmonic_load_flow_func():
    calc = 0
    for x in range(1000000):
        calc += x

def main_time_inside():
    for _ in range(10):
        harmonic_load_flow_func_time_inside()

def main_time_outside():   
    for _ in range(10):
        time1 = time.perf_counter()
        harmonic_load_flow_func()
        print('time: {}'.format(time.perf_counter() - time1))

if __name__ == '__main__':
    print("measuring time inside the function")
    main_time_inside()
    print("measuring time from outside")
    main_time_outside()

这篇关于如何在numba.jit函数中测量时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆