Python numba 的使用

  • 挺好,原作者太忙了,作为他的... 我来代替更一下。
  • 祝他半期不挂!

  • 谁曾侍秋风画扇。遮掩几时落寞。短梳牛角理红妆。阶前梧桐老,扉下虫丝长。
  • 点墨胸中话短长。南北天桥谁架?再相逢羞谈过往。今朝晨露好,融化笺上霜。

cProfile测试

  • 很好,这是一段测试代码
import cProfile
from time import sleep

def delay1():
    sleep(.5)
    
def delay2():
    sleep(1)
    
def simulate(Input):

    while_count = 10000000
    while while_count:
        while_count -= 1
        Input += 1
        
    for_count = 10000000
    for i in range(for_count):
        Input += 2

    for i in range(for_count):
        Input += 2


    delay1()
    delay2()
    delay2()
    delay2()
    delay2()
    
    return Input

  • 测试命令
>>>cProfile.run('simulate(21)')
  • 输出
         14 function calls in 5.770 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    1.241    1.241    5.770    5.770 0.py:11(simulate)
        1    0.000    0.000    0.503    0.503 0.py:5(delay1)
        4    0.000    0.000    4.026    1.006 0.py:8(delay2)
        1    0.000    0.000    5.770    5.770 <string>:1(<module>)
        1    0.000    0.000    5.770    5.770 {built-in method builtins.exec}
        5    4.529    0.906    4.529    0.906 {built-in method time.sleep}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  • ncall:函数运行次数
  • tottime: 函数的总的运行时间减去函数中调用子函数的运行时间 
  • 第一个percall:percall = tottime / nclall
  • cumtime:函数及其所有子函数调整的运行时间
  • 第二个percall:percall = cumtime / nclall 

numba的使用

  • 模拟函数
import cProfile
import time
from numba import jit

@jit
def simulate(Input):
    while_count = 10000000
    while while_count:
        while_count -= 1
        Input += 1
    for_count = 10000000
    for i in range(for_count):
        Input += 2
    for i in range(for_count):
        Input += 2
    return Input

start = time.time()
simulate(100)
end = time.time()
print(end-start)
  • 时间
0.31107425689697266
cProfile.run('simulate(100)')
         4 function calls in 0.000 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    0.000    0.000    0.000    0.000 0.py:5(simulate)
        1    0.000    0.000    0.000    0.000 <string>:1(<module>)
        1    0.000    0.000    0.000    0.000 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  • 未优化前
1.029198408126831
cProfile.run('simulate(100)')
         4 function calls in 1.234 seconds

   Ordered by: standard name

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1    1.234    1.234    1.234    1.234 0.py:6(simulate)
        1    0.000    0.000    1.234    1.234 <string>:1(<module>)
        1    0.000    0.000    1.234    1.234 {built-in method builtins.exec}
        1    0.000    0.000    0.000    0.000 {method 'disable' of '_lsprof.Profiler' objects}
  • @njit和@jit(nopython=True)具有相类似的功能

numba和numpy的结构体

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

River Chandler

谢谢,我会更努力学习工作的!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值