Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How Slow Are Your Loops?

Sponsored · Your Podcast. Everywhere. Effortlessly. Share. Educate. Inspire. Entertain. You do you. We'll handle the rest. →

How Slow Are Your Loops?

Some basic techniques to measure and improve the speed of your code.

Avatar for M. Emre Aydin

M. Emre Aydin

May 12, 2017

Other Decks in Programming

Transcript

  1. 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
  2. WAYS TO IMPROVE SPEED: MATHEMATICAL APPROXIMATIONS • The MorletWavelet •

    = %&'( )' − %+ ,& ⁄ • When c < 0.02, %+ ,& ⁄ is negligible, a.k.a. TheAbbreviated MorletWavelet (Foster, 1996) • () = )'%&'(
  3. CONVERTING CANON RAW FILES TO FITS FILES: CR2FITS Dcraw: Dave

    Coffin NetPbmFile: Christoph Gohlke Meredith Rawls • @merrdiff
  4. #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
  5. #5 DATE PARSING %854 (further) speed up! 105 ms to

    1.31 ms is more than 80x faster…
  6. 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