79
Writing Fast Code PyCon HK 2015 [email protected]

Writing Fast Code - PyCon HK 2015

Embed Size (px)

Citation preview

Page 1: Writing Fast Code - PyCon HK 2015

Writing Fast CodePyCon HK 2015

[email protected]

Page 2: Writing Fast Code - PyCon HK 2015

Younggun, Kim http://younggun.kim

@scari_netscari

Page 3: Writing Fast Code - PyCon HK 2015

Badass Alien @ District 9, SMARTSTUDY

http://pengpenghu.com

PyCon Korea Organizer http://pycon.kr @pyconkr PyCon APAC 2016 Host (2016/Aug/13-15)

Page 4: Writing Fast Code - PyCon HK 2015

What I Think My Code Run

Movie - The Good The Bad The Weird, 2008

Page 5: Writing Fast Code - PyCon HK 2015

How My Code Really Run

The Killers : All These Things That I’ve Done M/V https://youtu.be/sZTpLvsYYHw

Page 6: Writing Fast Code - PyCon HK 2015

Objective

Page 7: Writing Fast Code - PyCon HK 2015

1. Understanding How Computer Works

Page 8: Writing Fast Code - PyCon HK 2015

2. How to use Profiler

Page 9: Writing Fast Code - PyCon HK 2015

But why?

Page 10: Writing Fast Code - PyCon HK 2015

Say, thousands of people using your code everyday and if you save 1 second to run it, this means you could save over 4 days of time human race wasted per a year.

Page 11: Writing Fast Code - PyCon HK 2015

See How Computer Works and How Fast Computer

and it’s peripherals

Page 12: Writing Fast Code - PyCon HK 2015

I/O >> 4D Wall >> Memory

Page 13: Writing Fast Code - PyCon HK 2015

Morse Code Modem (2400) CDMA(2G) HSPA(3G, DL)LTE*USB 2.0802.11nUSB 3.0SATA 3.0Thunderbolt 2DDR2 1066MhzDDR3 1600Mhz

≈ 21 bps≈ 2400 bps≈ 153 kbit/s≈ 13.98 Mbit/s≈ 100 Mbit/s≈ 480 Mbit/s≈ 600 Mbit/s≈ 3 Gbit/s≈ 6 Gbit/s≈ 20 Gbit/s≈ 64 Gbit/s≈ 102.4 Gbit/s

https://en.wikipedia.org/wiki/List_of_device_bit_rates

Page 14: Writing Fast Code - PyCon HK 2015

Yes! Memory is blazing fast! (Really?)

Page 15: Writing Fast Code - PyCon HK 2015

DDR3 1600MhzFSB 400 (old Xeon)PCI Express 3.0 (x16)QuickPath InterconnectHyperTransport 3.1L3 Cache(i7-4790X)L2 Cache(i7-4790X)

≈ 12.8 GB/s≈ 12.8 GB/s≈ 16 GB/s≈ 38.4 GB/s≈ 51.2 GB/s≈ 170 GB/s≈ 308 GB/s

Nope!

Page 16: Writing Fast Code - PyCon HK 2015

Computer Knows Only 0 and 1

Page 17: Writing Fast Code - PyCon HK 2015

00100000001000100000000101011110

Like This

Page 18: Writing Fast Code - PyCon HK 2015

00100000001000100000000101011110opcode

addr 1

addr 2

value

MIPS32 Add Immediate instruction (ADDI)

addi $r1, $r2, 350

$r1 = $r2 + 350

Page 19: Writing Fast Code - PyCon HK 2015

Computer Execute These Instruction per clock basis

Page 20: Writing Fast Code - PyCon HK 2015

Clock (Hz)

Page 21: Writing Fast Code - PyCon HK 2015
Page 22: Writing Fast Code - PyCon HK 2015
Page 23: Writing Fast Code - PyCon HK 2015
Page 24: Writing Fast Code - PyCon HK 2015

1Hz

Page 25: Writing Fast Code - PyCon HK 2015

1Hz

L1 Cache AccesL2 Cache AccessL3 Cache AccessRAM AccessSSD I/OHDD I/OInternet: Tokyo to SFRun IPython (0.6s)Reboot (5m)

3s 9s

43s6m

2-6 days1-12 months

12 years63 years

32,000 years!!

Page 26: Writing Fast Code - PyCon HK 2015
Page 27: Writing Fast Code - PyCon HK 2015

Disassemble Python Code To CPython Bytecode To Support Analysis

dis module

https://docs.python.org/3/library/dis.html https://github.com/python/cpython/blob/master/Include/opcode.h

Page 28: Writing Fast Code - PyCon HK 2015
Page 29: Writing Fast Code - PyCon HK 2015

line # of source

op addr / instruction annotations

param

Page 30: Writing Fast Code - PyCon HK 2015

An Empty List Creation

[] vs list()

Page 31: Writing Fast Code - PyCon HK 2015
Page 32: Writing Fast Code - PyCon HK 2015
Page 33: Writing Fast Code - PyCon HK 2015
Page 34: Writing Fast Code - PyCon HK 2015

Dictionary

{} vs dict()

Page 35: Writing Fast Code - PyCon HK 2015
Page 36: Writing Fast Code - PyCon HK 2015
Page 37: Writing Fast Code - PyCon HK 2015
Page 38: Writing Fast Code - PyCon HK 2015

Find an element in a list

using for-loop vs in

Page 39: Writing Fast Code - PyCon HK 2015
Page 40: Writing Fast Code - PyCon HK 2015
Page 41: Writing Fast Code - PyCon HK 2015
Page 42: Writing Fast Code - PyCon HK 2015
Page 43: Writing Fast Code - PyCon HK 2015

A tool for dynamic program analysisthat measure the space or time

complexity of a program.

Profilers

Page 44: Writing Fast Code - PyCon HK 2015

• cProfile (profile) • hotshot • line_profiler • memory_profiler • yappi • profiling • pyinstrument • plop • pprofile

Page 45: Writing Fast Code - PyCon HK 2015

cProfile

• built-in profiling tool • hook into the VM in CPython • introduces overhead a bit

https://docs.python.org/3.5/library/profile.html

Page 46: Writing Fast Code - PyCon HK 2015

cProfile

python -m cProfile python_code.py

Page 47: Writing Fast Code - PyCon HK 2015

line_profiler

• can profile line-by-line basis • Uses a decorator to mark the

chosen function (@profile) • introduces greater overhead

https://github.com/rkern/line_profiler

Page 48: Writing Fast Code - PyCon HK 2015

profiling• Interactive Python profiler which

inspired from Unity3D Profiler • Keep the call stack. • Live Profiling • Only Support Linux

https://github.com/what-studio/profiling

Page 49: Writing Fast Code - PyCon HK 2015

https://github.com/sublee/pyconkr2015-profiling-resources/blob/master/continuous.gif

Page 50: Writing Fast Code - PyCon HK 2015

fibona

Use profiler with real code

Page 51: Writing Fast Code - PyCon HK 2015

fibonaKorean Fried Chicken Served as one chicken. (not pieces)

And it’s quite complex to determine how many chicken would enough for N people.

Page 52: Writing Fast Code - PyCon HK 2015

fibonaThe problem can be solved easily using fibonacci number.

1 1 2 3 5 8 13 21 34 …

For Nth fibonacci number of people, N-1 th fibonacci number of chicken would be perfect.

Page 53: Writing Fast Code - PyCon HK 2015

fibona

Awesome Idea! but how do you get enough chicken if number of the people is not an fibonacci number?

Page 54: Writing Fast Code - PyCon HK 2015

fibonaApply Zeckendorf’s theorem, which is about the representation of integers as sum of Fibonacci number

Page 55: Writing Fast Code - PyCon HK 2015

https://en.wikipedia.org/wiki/Zeckendorf's_theorem

Page 56: Writing Fast Code - PyCon HK 2015
Page 57: Writing Fast Code - PyCon HK 2015
Page 58: Writing Fast Code - PyCon HK 2015
Page 59: Writing Fast Code - PyCon HK 2015
Page 60: Writing Fast Code - PyCon HK 2015
Page 61: Writing Fast Code - PyCon HK 2015

KEEPCALMAND USE

THEPROFILER

Page 62: Writing Fast Code - PyCon HK 2015

cProfile

python -m cProfile fibonachicken.py

Page 63: Writing Fast Code - PyCon HK 2015

cProfile

Page 64: Writing Fast Code - PyCon HK 2015

line_profiler

Page 65: Writing Fast Code - PyCon HK 2015

line_profiler

kernprof -l -v fibonachicken.py

Page 66: Writing Fast Code - PyCon HK 2015

line_profiler

Page 67: Writing Fast Code - PyCon HK 2015

line_profiler

Page 68: Writing Fast Code - PyCon HK 2015

line_profiler

Page 69: Writing Fast Code - PyCon HK 2015

line_profiler

Page 70: Writing Fast Code - PyCon HK 2015

Both fib() and is_fibonacci() is the bottleneck. Should replace these with better one

Page 71: Writing Fast Code - PyCon HK 2015

Hypothesis #1

Improvement of fib() could result better performance

Page 72: Writing Fast Code - PyCon HK 2015

Binet’s Formula

https://en.wikipedia.org/wiki/Jacques_Philippe_Marie_Binet

Page 73: Writing Fast Code - PyCon HK 2015

cProfile

Page 74: Writing Fast Code - PyCon HK 2015

Hypothesis #2

Can we improve is_fibonacci() not to use fib() at all?

Page 75: Writing Fast Code - PyCon HK 2015

n is a Fibonacci number if and only if 5n*n+4 or 5n*n-4 is a square

Gessel’s Formula

http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fibonacci/fibFormula.html

Page 76: Writing Fast Code - PyCon HK 2015

cProfile

Page 77: Writing Fast Code - PyCon HK 2015

Summary

Consider efficiency of codes, along with peripherals, and circumstances around you

Form a hypothesis and confirm (using good profilers)

Page 78: Writing Fast Code - PyCon HK 2015

QA

Page 79: Writing Fast Code - PyCon HK 2015

Thanks!