Python中国社区  »  Python

什么,你嫌Python跑得慢?

生信媛 • 2 周前 • 97 次点击  

Speed Up Your Scripts

Why so slow?
  1. Python is a interpreted advanced compute language, run with cpython 's Interpreter, no like compiled language such as C++ et al.

  2. Python 's Global Interpreter Lock(GIL).In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe. (However, since the GIL exists, other features have grown to depend on the guarantees that it enforces.)

简单说,Python作为解释型高级编程语言,需要在运行时动态分配内存,这消耗一定时间,并且还有全局解释锁(Global interpreter Lock,GIL),玩不了原生的多线程。--By 日更翻译

Some good ways to speed your scripts
  1. to be more pythonic, using standard library such as numpy, scipy, pandas  

  2. using speed up modules such as Cython, numba,  numexpr, PyJulia, Pygo

  3. using 3rd party Interpreter such as pypy      

多用标准库以及Numpy, scipy, pandas,答应我,Pythonic一点好吗?使用CPython,numba, numexpr, PyJulia, Pygo加速利用第三方解释器,如pypy--By 日更翻译

numba(without using GPU) vs None on windows
#!python 
# coding:utf-8
# author:kim
# copyrights 2017 www.lowpitch.cn all rights reserved.
import time
from numba import jit

def timer(func):
   def wrapper(*args, **kwargs):
       start= time.time()
       funcr=func()
       print("time use: %s" %(time.time()-start))
       return funcr
   return wrapper

# @timer
def main1(x):
   start = time.time()
   count = len(x)
   for i in range(count):
       for j in range(i+1,count):
           if x[i]>x[j]:
               x[i], x[j]=x[j], x[i]
   print("time use: %s" %(time.time()-start))
   return x

@jit()
def main2(x):
   start = time.time()
   count = len(x)
   for i in range(count):
       for j in range(i+1,count):
           if x[i]>x[j]:
               x[i], x[j]=x[j], x[i]
   print("time use: %s" %(time.time()-start))
   return x

if __name__ == '__main__':
   x=[i for i in range (10000)]
   x.reverse()
   main1(x)
   main2(x)

results


[Running] D://python35//python.exe "c:\Users\Kim\Desktop\tougao\speedup\test.py"
time use: 24.19657588005066
time use: 0.34252214431762695

[Done] exited with code=0 in 26.282 seconds

python3 vs pypy3


import time
from numba import jit

def timer(func):
   def wrapper(*args, **kwargs):
       start= time.time()
       func(*args, **kwargs)
       print("time use: %s" %(time.time()-start))
   return wrapper()

@timer
def main1(x):
   count = len(x)
   for i in range(count):
       for j in range(i+1,count):
           if x[i]>x[j]:
               x[i], x[j]=x[j], x[i]
   return x

if __name__ == '__main__':
   x=[i for i in range (10000)]
   x.reverse()
   main1(x)

results

[kim@bio ~]
$ pypy3 test.py
time use: 0.10715413093566895
[kim@bio ~]
$ python3 test.py
time use: 5.735241651535034

Thanks
  • pypi

  • numba

  • pypy

  • cython  



今天看啥 - 高品质阅读平台
本文地址:http://www.jintiankansha.me/t/Kz2MZQaKhA
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/4379
 
97 次点击  
分享到微博
分享
社区所有版块导航
Python
python开源   Python   DjangoApp   pycharm   Django  
DATA
Elasticsearch   docker  
WEB开发
linux   MongoDB   bottle   tornado   Redis   NoSql   IE   DATABASE   MQ   Bootstrap   NGINX   js   其他Web框架   Jquery   peewee   web工具   Git   zookeeper  
机器学习
机器学习算法  
Python88.com
社区推广   反馈   公告