SlideShare a Scribd company logo
1 of 11
Download to read offline
使用Numba加速Python应用
吴江
2018-10-21
1 / 11
题目
打印 11 到 1000 之间,10 进制、2 进制和 8 进制都是回文的数字。
Benchmark: https://github.com/nouse/python-go-simple-benchmark
2 / 11
实现
def is_palindrome(s):
return all(a==b for a,b in itertools.zip_longest(
s, reversed(s)))
def palindrome_number():
for i in CANDIDATES:
if is_palindrome(f'{num:o}') and is_palindrome(
f'{num:b}'):
return -1
3 / 11
第一轮测试
Platform Kops/s
Python 3.7 23.15
PyPy 3.5.3 204.29
Go 1.11.1 746.26
4 / 11
优化:使用整数算法
def palindrome_number2() -> int:
for num in CANDIDATES:
if num == reverse8(num) and num == reverse2(num):
return num
return -1
5 / 11
第二轮测试
Platform Kops/s
Python 3.7 34.10
PyPy 3.5.3 470.56
Go 1.11.1 6211
PyPy 优化不足
6 / 11
Numba
from numba import jit, int32
@jit(int32())
def palindrome_number2() -> int:
for num in CANDIDATES:
if num == reverse8(num) and num == reverse2(num):
return num
return -1
7 / 11
第三轮测试
Platform Kops/s
Python 3.7 34.10
PyPy 3.5.3 470.56
Numba 4769
Go 1.11.1 6211
8 / 11
Numba其他特点
▶ @jitclass
▶ Parallelize
▶ Vectorize
▶ GPU backend(CUDA and AMD HSA)
Homepage: http://numba.pydata.org/
9 / 11
其他工具
Cython https://cython.org/
Pythran https://pythran.readthedocs.io
10 / 11
Thanks!
11 / 11

More Related Content

What's hot (20)

Polar example
Polar examplePolar example
Polar example
 
Ppt 78-100
Ppt 78-100Ppt 78-100
Ppt 78-100
 
Ppt 120-126
Ppt 120-126Ppt 120-126
Ppt 120-126
 
Ppt 78-100
Ppt 78-100Ppt 78-100
Ppt 78-100
 
Appendix B
Appendix BAppendix B
Appendix B
 
Ch6 函式
Ch6 函式Ch6 函式
Ch6 函式
 
P127 135 new
P127 135 newP127 135 new
P127 135 new
 
Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍Ihome inaction 篇外篇之fp介绍
Ihome inaction 篇外篇之fp介绍
 
Ppt 127-135
Ppt 127-135Ppt 127-135
Ppt 127-135
 
Ppt 1-25
Ppt 1-25Ppt 1-25
Ppt 1-25
 
Ppt 51-77
Ppt 51-77Ppt 51-77
Ppt 51-77
 
Ppt 1-50
Ppt 1-50Ppt 1-50
Ppt 1-50
 
Ch1 c++總複習
Ch1 c++總複習Ch1 c++總複習
Ch1 c++總複習
 
C python 原始碼解析 投影片
C python 原始碼解析 投影片C python 原始碼解析 投影片
C python 原始碼解析 投影片
 
Ch 8
Ch 8Ch 8
Ch 8
 
Ppt 26-50
Ppt 26-50Ppt 26-50
Ppt 26-50
 
Sym py edu
Sym py eduSym py edu
Sym py edu
 
Ch1 教學
Ch1 教學Ch1 教學
Ch1 教學
 
偷偷學習 Python3
偷偷學習 Python3偷偷學習 Python3
偷偷學習 Python3
 
Ch12 範例
Ch12 範例Ch12 範例
Ch12 範例
 

Similar to Python speed up with numba

Metro Style Apps from C++ Developers' View
Metro Style Apps from C++ Developers' ViewMetro Style Apps from C++ Developers' View
Metro Style Apps from C++ Developers' ViewEric ShangKuan
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算建興 王
 
Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享happyagan
 
程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號鍾誠 陳鍾誠
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记Lingfei Kong
 
ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxNCU MCL
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptxmclmath
 
少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月鍾誠 陳鍾誠
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集shademoon
 
MPI use c language
MPI use c languageMPI use c language
MPI use c languageZongYing Lyu
 
ncuma_Taylor 多項式.pptx
ncuma_Taylor 多項式.pptxncuma_Taylor 多項式.pptx
ncuma_Taylor 多項式.pptxNCU MCL
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhouWill Zhou
 
ncuma_函式.pptx
ncuma_函式.pptxncuma_函式.pptx
ncuma_函式.pptxNCU MCL
 
Go语言: 互联网时代的C
Go语言: 互联网时代的CGo语言: 互联网时代的C
Go语言: 互联网时代的CGoogol Lee
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Derek Lee
 

Similar to Python speed up with numba (20)

Metro Style Apps from C++ Developers' View
Metro Style Apps from C++ Developers' ViewMetro Style Apps from C++ Developers' View
Metro Style Apps from C++ Developers' View
 
Ch9 教學
Ch9 教學Ch9 教學
Ch9 教學
 
Ch9
Ch9Ch9
Ch9
 
認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算認識 C++11 新標準及使用 AMP 函式庫作平行運算
認識 C++11 新標準及使用 AMP 函式庫作平行運算
 
Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享Linux Tracing System 浅析 & eBPF框架开发经验分享
Linux Tracing System 浅析 & eBPF框架开发经验分享
 
程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號程式人雜誌 -- 2015 年9月號
程式人雜誌 -- 2015 年9月號
 
Python学习笔记
Python学习笔记Python学习笔记
Python学习笔记
 
Python變數與資料運算
Python變數與資料運算Python變數與資料運算
Python變數與資料運算
 
ncuma_函數微分計算.pptx
ncuma_函數微分計算.pptxncuma_函數微分計算.pptx
ncuma_函數微分計算.pptx
 
函數微分_範例.pptx
函數微分_範例.pptx函數微分_範例.pptx
函數微分_範例.pptx
 
少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月少年科技人雜誌 2015 年八月
少年科技人雜誌 2015 年八月
 
第六章 函數與巨集
第六章 函數與巨集第六章 函數與巨集
第六章 函數與巨集
 
MPI use c language
MPI use c languageMPI use c language
MPI use c language
 
ncuma_Taylor 多項式.pptx
ncuma_Taylor 多項式.pptxncuma_Taylor 多項式.pptx
ncuma_Taylor 多項式.pptx
 
Learning python in the motion picture industry by will zhou
Learning python in the motion picture industry   by will zhouLearning python in the motion picture industry   by will zhou
Learning python in the motion picture industry by will zhou
 
ncuma_函式.pptx
ncuma_函式.pptxncuma_函式.pptx
ncuma_函式.pptx
 
Go语言: 互联网时代的C
Go语言: 互联网时代的CGo语言: 互联网时代的C
Go语言: 互联网时代的C
 
Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18Python入門:5大概念初心者必備 2021/11/18
Python入門:5大概念初心者必備 2021/11/18
 
Ch10
Ch10Ch10
Ch10
 
Ch10 教學
Ch10 教學Ch10 教學
Ch10 教學
 

More from Jiang Wu

Implement Web API with Swagger
Implement Web API with SwaggerImplement Web API with Swagger
Implement Web API with SwaggerJiang Wu
 
API documentation with Swagger UI(LT)
API documentation with Swagger UI(LT)API documentation with Swagger UI(LT)
API documentation with Swagger UI(LT)Jiang Wu
 
用Ruby编写博客应用
用Ruby编写博客应用用Ruby编写博客应用
用Ruby编写博客应用Jiang Wu
 
Sinatra and friends
Sinatra and friendsSinatra and friends
Sinatra and friendsJiang Wu
 
Rubyconf China
Rubyconf ChinaRubyconf China
Rubyconf ChinaJiang Wu
 
Ruby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelRuby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelJiang Wu
 

More from Jiang Wu (7)

Implement Web API with Swagger
Implement Web API with SwaggerImplement Web API with Swagger
Implement Web API with Swagger
 
API documentation with Swagger UI(LT)
API documentation with Swagger UI(LT)API documentation with Swagger UI(LT)
API documentation with Swagger UI(LT)
 
用Ruby编写博客应用
用Ruby编写博客应用用Ruby编写博客应用
用Ruby编写博客应用
 
Sinatra and friends
Sinatra and friendsSinatra and friends
Sinatra and friends
 
Rubyconf China
Rubyconf ChinaRubyconf China
Rubyconf China
 
JS2
JS2JS2
JS2
 
Ruby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequelRuby off Rails---rack, sinatra and sequel
Ruby off Rails---rack, sinatra and sequel
 

Python speed up with numba