numba加速python学习与尝试
学习:人工智能-机器学习-深度学习概念的区别

使用numba的姿势不正确反而导致性能下降

妙音 posted @ 2019年1月20日 09:24 in 人工智能 with tags python 性能 , 2186 阅读
numba能够极大的提高python在计算方面的性能。是不是所有的python代码上,都可以加上numba.jit装饰器?答案是否定的。
 
 
 

示例

 

环境

 
python3.6
fedora
pymysql
 

示例

 
很常见的例子,从数据库从查询一千条数据,再进行简单的格式转换。
 
#coding=utf-8
import time
from numba import jit

import pymysql
conn = pymysql.connect("192.168.10.125", "username", "password", "db_name")

#@jit   #位置1
def test_for():
    c =conn.cursor()
    c.execute("select * from user limit 1000")

    start = time.time()
    for a in c.fetchall():
        to_dict(a, c.description)
    end = time.time()
    print("cost_seconds:", end-start)

#@jit   #位置2
def to_dict(row, description):
    """
    记录转为字典
    """
    item = dict()
    for i, field in enumerate(description):
        field_name = field[0]
        item[field_name] = row[i]
    return item

test_for()
 

 结果

 
python3           0.0033788681030273438
python3 +位置1jit 0.08921098709106445
python3 + 位置1jit+位置2jit  3.846426010131836
pypy              0.0065860748291015625
 
对比结果,发现如果是非计算问题,引入即时编译,反而导致性能下降。实际python在非计算问题上性能很好。
 
 

来源

 

 

Avatar_small
Latest foreign music 说:
2019年9月02日 03:57

It is very good, but look at the information at this address.

Avatar_small
satta king result 说:
2019年10月26日 20:24

It has fully emerged to crown Singapore's southern shores and undoubtedly placed her on the global map of residential landmarks. I still scored the more points than I ever have in a season for GS. I think you would be hard pressed to find somebody with the same consistency I have had over the years so I am happy with that.

Avatar_small
f88tw 说:
2019年10月27日 10:35

敬启者:个人小网站希望大家多多支持 感谢您对我们热心的支持 f88tw┃华歌尔┃I appreciate your kind assistance. http://mypaper.pchome.com.tw/f88tw/post/1370781143 Wacoal <a data-cke-saved-href="http://mypaper.pchome.com.tw/f88tw" href="http://mypaper.pchome.com.tw/f88tw" "="" style="box-sizing: border-box; background: 0px 0px rgb(255, 255, 255); color: rgb(66, 139, 202); text-decoration-line: none; transition: all 0.1s ease-in-out 0s; font-family: &quot;Roboto Condensed&quot;, sans-serif; font-size: 14px;">http://mypaper.pchome.com.tw/f88tw http://mypaper.pchome.com.tw/f88tw/P1 http://mypaper.pchome.com.tw/f88tw/post/1379131888 禁忌|捡骨师|新竹|时间|台北|桃园|苗栗|头份

Avatar_small
Birds of Prey Costum 说:
2019年10月27日 13:11

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.

Avatar_small
Top Gun Maverick Jac 说:
2019年10月28日 19:27

I really enjoy simply reading all of your weblogs. Simply wanted to inform you that you have people like me who appreciate your work. Definitely a great post. Hats off to you! The information that you have provided is very helpful.

Avatar_small
https://nextdaygarag 说:
2019年11月07日 01:36

it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity..

Avatar_small
roll up garage doors 说:
2019年11月07日 01:37

Nice blog and absolutely outstanding. You can do something much better but i still say this perfect.Keep trying for the best.

Avatar_small
prize bond 1500 说:
2020年3月26日 16:24

Truly, this article is really one of the very best in the history of articles. I am a antique ’Article’ collector and I sometimes read some new articles if I find them interesting. And I found this one pretty fascinating and it should go into my collection. Very good work!

Avatar_small
james 说:
2020年3月27日 18:52

wow this saintly however ,I love your enter plus nice pics might be part personss negative love being defrent mind total poeple , movie123

Avatar_small
james 说:
2020年3月28日 04:15

Profit primarily prime quality items -- you can understand them all within: dewapoker

Avatar_small
poker88 说:
2020年3月29日 01:42

Link alternatif haruslah link yang dapat dipercaya dan dipastikan RESMI merupakan link dari website Poker88, maka dari itu link alternatif yang tertera disini adalah link yang benar, tepat dan Resmi. Link alter natif tersebut adalah
<a href="http://194.31.53.108/">poker88</a>


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter