• Python numba 的使用


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

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

    cProfile测试

    • 很好,这是一段测试代码
    1. import cProfile
    2. from time import sleep
    3. def delay1():
    4. sleep(.5)
    5. def delay2():
    6. sleep(1)
    7. def simulate(Input):
    8. while_count = 10000000
    9. while while_count:
    10. while_count -= 1
    11. Input += 1
    12. for_count = 10000000
    13. for i in range(for_count):
    14. Input += 2
    15. for i in range(for_count):
    16. Input += 2
    17. delay1()
    18. delay2()
    19. delay2()
    20. delay2()
    21. delay2()
    22. return Input

    • 测试命令
    >>>cProfile.run('simulate(21)')
    • 输出
    1. 14 function calls in 5.770 seconds
    2. Ordered by: standard name
    3. ncalls tottime percall cumtime percall filename:lineno(function)
    4. 1 1.241 1.241 5.770 5.770 0.py:11(simulate)
    5. 1 0.000 0.000 0.503 0.503 0.py:5(delay1)
    6. 4 0.000 0.000 4.026 1.006 0.py:8(delay2)
    7. 1 0.000 0.000 5.770 5.770 <string>:1(<module>)
    8. 1 0.000 0.000 5.770 5.770 {built-in method builtins.exec}
    9. 5 4.529 0.906 4.529 0.906 {built-in method time.sleep}
    10. 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的使用

    • 模拟函数
    1. import cProfile
    2. import time
    3. from numba import jit
    4. @jit
    5. def simulate(Input):
    6. while_count = 10000000
    7. while while_count:
    8. while_count -= 1
    9. Input += 1
    10. for_count = 10000000
    11. for i in range(for_count):
    12. Input += 2
    13. for i in range(for_count):
    14. Input += 2
    15. return Input
    16. start = time.time()
    17. simulate(100)
    18. end = time.time()
    19. print(end-start)
    • 时间
    1. 0.31107425689697266
    2. cProfile.run('simulate(100)')
    3. 4 function calls in 0.000 seconds
    4. Ordered by: standard name
    5. ncalls tottime percall cumtime percall filename:lineno(function)
    6. 1 0.000 0.000 0.000 0.000 0.py:5(simulate)
    7. 1 0.000 0.000 0.000 0.000 <string>:1(<module>)
    8. 1 0.000 0.000 0.000 0.000 {built-in method builtins.exec}
    9. 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
    • 未优化前
    1. 1.029198408126831
    2. cProfile.run('simulate(100)')
    3. 4 function calls in 1.234 seconds
    4. Ordered by: standard name
    5. ncalls tottime percall cumtime percall filename:lineno(function)
    6. 1 1.234 1.234 1.234 1.234 0.py:6(simulate)
    7. 1 0.000 0.000 1.234 1.234 <string>:1(<module>)
    8. 1 0.000 0.000 1.234 1.234 {built-in method builtins.exec}
    9. 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
    • @njit和@jit(nopython=True)具有相类似的功能

    numba和numpy的结构体

  • 相关阅读:
    Linux多进程(二)进程通信方式三 共享内存
    08 获取器 withAttr、多连缀、whereRaw、事务、数据集《ThinkPHP6 入门到电商实战》
    git@github.com: Permission denied (publickey).
    【(数据结构)— 单链表的实现】
    RabbitMQ基础组件笔记
    一天一块钱,月月换新包商业模式
    简单工厂模式、工厂模式、抽象工厂模式(含C++代码)
    音视频开发进阶——YUV与RGB的采样与存储格式
    管理多个项目的 Git 配置文件
    算法 之 链表
  • 原文地址:https://blog.csdn.net/Chandler_river/article/details/127680802