国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

使用numba對Python運算加速的方法

2020-02-15 23:13:54
字體:
來源:轉載
供稿:網友

有時候需要比較大的計算量,這個時候Python的效率就很讓人捉急了,此時可以考慮使用numba 進行加速,效果提升明顯~

(numba 安裝貌似很是繁瑣,建議安裝Anaconda,里面自帶安裝好各種常用科學計算庫)

from numba import jit@jitdef t(count=1000): total = 0 for i in range(int(count)):  total += i return total

測試效果:

(關于__wrapped__ 見我的博文: 淺談解除裝飾器作用(python3新增) )

In [17]: %timeit -n 1 t.__wrapped__()1 loop, best of 3: 52.9 µs per loopIn [18]: %timeit -n 1 t()The slowest run took 13.00 times longer than the fastest. This could mean that an intermediate result is being cached.1 loop, best of 3: 395 ns per loop

可以看到使用jit 加速后,即使設置測試一次,實際上還是取了三次的最優值,如果取最壞值(因為最優值可能是緩存下來的),則耗時為395ns * 13 大概是5us 還是比不使用的52.9us 快上大概10倍,

增大計算量可以看到使用numba加速后的效果提升更加明顯,

In [19]: %timeit -n 10 t.__wrapped__(1e6)10 loops, best of 3: 76.2 ms per loopIn [20]: %timeit -n 1 t(1e6)The slowest run took 8.00 times longer than the fastest. This could mean that an intermediate result is being cached.1 loop, best of 3: 790 ns per loop

如果減少計算量,可以看到當降到明顯小值時,使用加速后的效果(以最差計)與不加速效果差距不大,因此如果涉及到較大計算量不妨使用jit 加速下,何況使用起來這么簡便。

%timeit -n 1 t(10)1 loop, best of 3: 0 ns per loop%timeit -n 100 t.__wrapped__(10)100 loops, best of 3: 1.79 µs per loop%timeit -n 1 t(1)The slowest run took 17.00 times longer than the fastest. This could mean that an intermediate result is being cached.1 loop, best of 3: 395 ns per loop%timeit -n 100 t.__wrapped__(1)100 loops, best of 3: 671 ns per loop

以上這篇使用numba對Python運算加速的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 将乐县| 利川市| 闽清县| 安义县| 古丈县| 九龙坡区| 介休市| 陈巴尔虎旗| 浦东新区| 池州市| 洪雅县| 正定县| 武冈市| 承德市| 淅川县| 柳江县| 蒲城县| 胶州市| 鹤壁市| 太湖县| 柳河县| 泰来县| 共和县| 高阳县| 澄城县| 梅河口市| 玛曲县| 邵东县| 嘉禾县| 朝阳区| 成武县| 辛集市| 甘孜县| 张北县| 永昌县| 湖南省| 南城县| 巩留县| 宜昌市| 凤山市| 九江市|