Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
How Slow Are Your Loops?
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
M. Emre Aydin
May 12, 2017
Programming
39
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
How Slow Are Your Loops?
Some basic techniques to measure and improve the speed of your code.
M. Emre Aydin
May 12, 2017
Other Decks in Programming
See All in Programming
SREの越境 / SRE Collaboration
y0hgi
2
310
自サークルのIdPからログアウトできるようになるまで
taiseiue
1
140
AIに “答え“を書かせるのを やめてみた
___akari11
0
120
作れる、でも消せるか?──安全に「枯らして消す」機能削除術 / Drain, Then Delete
chobishiba
2
640
機械に任せるテスト、人が見るテスト ⽣成AIで引き直した、運⽤保守フェーズの線引き
dske104
0
190
contenteditable と日本語入力に向き合う
colorful12
0
200
月間100万人のサービス刷新で、 技術選定をHonoに振り切った話
yutobara
8
6.6k
IBM Bob Dojo #1 仕様駆動開発入門
oniak3ibm
PRO
0
390
Unity6 URPによる海底をテーマにしたVJシステムの解説 | Unity Shader 完全に理解した 勉強会
gam0022
0
370
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
610
Railsのためのパスキー実践入門 〜マルチテナントSaaSでの意思決定〜
utakaha
2
550
Featured
See All Featured
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
630
職位にかかわらず全員がリーダーシップを発揮するチーム作り / Building a team where everyone can demonstrate leadership regardless of position
madoxten
69
67k
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.2k
Code Reviewing Like a Champion
maltzj
529
40k
Designing Experiences People Love
moore
143
24k
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
280
コードの90%をAIが書く世界で何が待っているのか / What awaits us in a world where 90% of the code is written by AI
rkaga
63
46k
[RailsConf 2023] Rails as a piece of cake
palkan
59
7.1k
Large-scale JavaScript Application Architecture
addyosmani
516
110k
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
540
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
0
270
Building Better People: How to give real-time feedback that sticks.
wjessup
370
20k
Transcript
HOW SLOW ARE YOUR LOOPS? M. Emre Aydın Python in
Astronomy, Leiden - 2017
HOW FAST ARE YOUR LOOPS?
HOW SLOW ARE YOUR LOOPS?
None
THE ROOT OF ALL EVIL • If you process live
data and your code can’t keep up… it’s slow. • If you find yourself squeezing in new hobbies while your code is “working”, it’s probably slow. xkcd: 303
WAYS TO IMPROVE SPEED: BETTER ALGORITHMS
WAYS TO IMPROVE SPEED: MATHEMATICAL APPROXIMATIONS • The MorletWavelet •
= %&'( )' − %+ ,& ⁄ • When c < 0.02, %+ ,& ⁄ is negligible, a.k.a. TheAbbreviated MorletWavelet (Foster, 1996) • () = )'%&'(
WAYS TO IMPROVE SPEED: OPTIMIZED COMPILERS • Cython • Numba
• PyPy
WAYS TO IMPROVE SPEED: OPTIMIZED COMPILERS • https://github.com/sanand0 • https://youtu.be/ylBslijJexw
WAYS TO IMPROVE SPEED: OPTIMIZED COMPILERS https://eng.climate.com/2015/04/09/numba-vs-cython-how-to-choose/
WAYS TO IMPROVE SPEED: OS & HARDWARE
CONVERTING CANON RAW FILES TO FITS FILES: CR2FITS Dcraw: Dave
Coffin NetPbmFile: Christoph Gohlke
CONVERTING CANON RAW FILES TO FITS FILES: CR2FITS Dcraw: Dave
Coffin NetPbmFile: Christoph Gohlke Meredith Rawls • @merrdiff
mount -t tmpfs -o size=512m tmpfs /mnt/tempfiles
COUNTING LINES OF A TEXT FILE
None
None
MEASURING TIME PYTHON 3.5.1 IPYTHON 5.1.0 xkcd: 1190, frame 403
TIMEIT MAGIC
LINE PROFILER
LINE PROFILER
None
None
MEMORY PROFILER
CHECK WHAT’S UNDER THE HOOD
#1 USE BUILT IN FUNCTIONS https://docs.python.org/3/library/functions.html
#2 FUNCTION CALL OVERHEAD https://www.python.org/doc/essays/list2str/ 1.72 s per loop 651
ms per loop “ […] function call overhead in Python is bigger than for loop overhead.” – Guido van Rossum
BYTECODE DISASSEMBLER
BYTECODE DISASSEMBLER http://akaptur.com/blog/2013/08/14/python- bytecode-fun-with-dis/ Allison Kaptur
#3 LIST COMPREHENSION
#3 LIST COMPREHENSION 15% speed up http://blog.cdleary.com/2010/04/efficiency-of-list-comprehensions/
#4 USE GENERATORS
#4 USE GENERATORS https://youtu.be/EnSu9hHGq5o Loop like a native: while, for,
iterators, generators Ned Batchelder
#5 DATE PARSING
#5 DATE PARSING
#5 DATE PARSING %2 speed up!
#5 DATE PARSING %2 speed up! Let’s use datetime?
#5 DATE PARSING
#5 DATE PARSING %910 speed up!
#5 DATE PARSING %910 speed up! Can we get rid
of strptime?
#5 DATE PARSING %854 (further) speed up! 105 ms to
1.31 ms is more than 80x faster…
#6 IS TRUE?
#7 TRY / EXCEPT
#7 TRY / EXCEPT
#7 TRY / EXCEPT
#8 GO LOCAL Sebastian Witowski : https://youtu.be/YjHsOrOOSuI
#9 USE NUMPY / PANDAS https://youtu.be/EEUXKG97YRw
#9 USE NUMPY / PANDAS BUT TEST IT! https://youtu.be/ylBslijJexw
WRAP UP • Don’t over-optimize! • Time it and profile
it • Start with the most inner loop • Consider list comprehension, generator, and numpy arrays • Take leverage of your OS: On the shoulders of giants