Sebastian Witowski - Writing faster Python

Ғылым және технология

Sebastian Witowski - Writing faster Python
[EuroPython 2016]
[19 July 2016]
[Bilbao, Euskadi, Spain]
(ep2016.europython.eu//conference/talks/writing-faster-python)
Presentation on how you can write faster Python in your daily work. I
will briefly explain ways of profiling the code, discuss different
code structures and show how they can be improved. You will see what
is the fastest way to remove duplicates from a list, what is faster
than a _for_ loop or how “asking for permission” is slower than
“begging for forgiveness”.
-----
Did you know that Python preallocates integers from -5 to 257 ?
Reusing them 1000 times, instead of allocating memory for a bigger
integer, can save you a couple of milliseconds of code’s execution
time. If you want to learn more about this kind of optimizations then,
… well, probably this presentation is not for you :) Instead of going
into such small details, I will talk about more _"sane"_ ideas for
writing faster code.
After a very brief overview of how to optimize Python code (rule 1:
don’t do this, rule 2: don’t do this yet, rule 3: ok, but what if I
really want to do this ?), I will show simple and fast ways of
measuring the execution time and finally, discuss examples of how some
code structures could be improved.
You will see:
- What is the fastest way of removing duplicates from a list
- How much faster your code is when you reuse the built-in functions
instead of trying to reinvent the wheel
- What is faster than the good ol’ _for_ loop
- If the lookup is faster in a list or a set (and when it makes sense
to use each)
- How the “It's better to beg for forgiveness than to ask for
permission” rule works in practice
I will NOT go into details of _"serious"_ optimization, like using
different Python implementation or rewriting critical code in C, etc.

Пікірлер: 60

  • @mateuszkurowski4510
    @mateuszkurowski45105 жыл бұрын

    One more thing the guy has missed: watching KZread videos at x2 speed makes you write faster Python

  • @juanok2775

    @juanok2775

    3 жыл бұрын

    true that

  • @vermaprashant15
    @vermaprashant153 жыл бұрын

    Great conferences. I liked and watched it at 1.75x to 2x 😂

  • @nelsonjma
    @nelsonjma6 жыл бұрын

    excellent ideas, thanks for the information.

  • @NirHaramati
    @NirHaramati5 жыл бұрын

    Brilliant! Thanks.

  • @LaurentLaborde
    @LaurentLaborde3 жыл бұрын

    FINALLY a performance oriented talk that actually helped me ! now i hate python slightly less that an hour ago :D

  • @sheikhakbar2067
    @sheikhakbar20673 жыл бұрын

    That was a good video to listen to while driving to buy falafel! I finished eating them and I have five more minutes to listen to!

  • @ukrainian333
    @ukrainian3332 жыл бұрын

    I'm was looking for this kind of video for years...

  • @prat-man
    @prat-man4 жыл бұрын

    I needed this

  • @twnfaem
    @twnfaem4 жыл бұрын

    Thanks a lot Sebastian! A bunch of useful stuff in there, indeed!

  • @macieklech1657
    @macieklech16577 жыл бұрын

    Great talk. A lot of useful stuff

  • @name6297
    @name62974 жыл бұрын

    12:05 would range function be a better option ?

  • @smackronme
    @smackronme7 жыл бұрын

    skip to 6:15

  • @NobleSteed00

    @NobleSteed00

    4 жыл бұрын

    better yet skip to 24:08

  • @marcd4144
    @marcd41445 жыл бұрын

    You know you're big shit when you're walking around on stage with no shoes on at a Dev event. lol

  • @broccoloodle
    @broccoloodle3 жыл бұрын

    Weren't most of them optimized with JIT compiling alr?

  • @MrLehoczky

    @MrLehoczky

    3 жыл бұрын

    CPython uses AOT compiler instead of JIT. However there is an alternative language implementation called PyPy with JIT compilation but it's not that widely used

  • @pemessh
    @pemessh3 жыл бұрын

    Couple of the slides had code that used , try except block, and in except block there was 'pass'. Please do not do this. At least log something silently. That way you know what's going on and it works too.

  • @sorry4all

    @sorry4all

    3 жыл бұрын

    Definitely. It's the best way to hide an error

  • @BryanJenks
    @BryanJenks2 жыл бұрын

    i know this is a man of culture because his code is in Gruvbox 🤣

  • @vwwvwwwvwwwvwwvvwwvw
    @vwwvwwwvwwwvwwvvwwvw7 жыл бұрын

    what does the % before timeit do? as in %timeit

  • @julioargumedo6722

    @julioargumedo6722

    7 жыл бұрын

    It's a magic function from the Ipython

  • @RoamingAdhocrat
    @RoamingAdhocrat3 жыл бұрын

    Using comic sans in the chyron makes for faster code.

  • @willemvdk4886
    @willemvdk48863 жыл бұрын

    I started skipping the first few minutes because I need my answers faster.

  • @JoshuaShope
    @JoshuaShope6 жыл бұрын

    Not fast because Python2

  • @JakubArnold
    @JakubArnold6 жыл бұрын

    At 12:40 ... 222ms vs 127ms and he says 75% faster?

  • @valentinkhomuteno6825

    @valentinkhomuteno6825

    6 жыл бұрын

    222 is 175% of 127, I think that what he meant. And he is right, in a way.

  • @tomriddle2257

    @tomriddle2257

    5 жыл бұрын

    I don't get the problem either. It's correct imo or how would you calculate it?

  • @flamendless
    @flamendless2 жыл бұрын

    Instead of optimizing, just by a faster hardware? What 😂

  • @keithcu2
    @keithcu26 жыл бұрын

    Great, but he should have talked about Cython as an option instead of C/C++.

  • @user-sw9kd9pv4n
    @user-sw9kd9pv4n4 жыл бұрын

    Don't agree with everything thing - Writhing performent code should be a habit not a after thought. Writhing code without performance in mind and then trying to optimise it later is not the best strategy, sometime it may not even work. It may require too much of design change, too much re-testing. Write optimum code from the beginning. Profile later to see if any small tweaks are required. I have see bad design where people were talking abt. h/w upgrades. Looked at the code, changed the design ...12 hr. execution time reduced to 3 mins.

  • @rockybangalore3830
    @rockybangalore38304 жыл бұрын

    Witing faster python, skip the aplhabets

  • @NTTCode
    @NTTCode2 жыл бұрын

    Great job but after that Python is still very slow.

  • @TheGodSaw
    @TheGodSaw4 жыл бұрын

    to write fast python. never write any algo yourself. Also, if you are using a for loop chances are you are doing something wrong. Got it.

  • @ds-mi4od

    @ds-mi4od

    4 жыл бұрын

    Godsaw this is a joke right

  • @ingframin
    @ingframin7 жыл бұрын

    "if you want your code to run faster you can start getting faster hardware in a first place"... Come on really? -.-"

  • @eljapel

    @eljapel

    6 жыл бұрын

    hardware is cheaper.

  • @LashyYT
    @LashyYT5 жыл бұрын

    I’m a beginner with python Print (“hello world”) Hello world A = ‘hello’ A Hello

  • @lionpersia
    @lionpersia5 жыл бұрын

    Didn't expect such a high-pitch voice from such a large guy.

  • @sivasankar2784
    @sivasankar27842 жыл бұрын

    The audience not laughing at this first joke must be demoralizing af.

  • @Penta_Penguin_237
    @Penta_Penguin_2372 жыл бұрын

    Such a loser mentality. Most times faster code is just faster code. Often even more stable. Use C compiled libraries such as Numpy or accelerators like Numba has no downsides

  • @lepidoptera9337
    @lepidoptera93373 жыл бұрын

    Oh, boy, that was a waste of time. Picking faster hardware rather than a faster algorithm? In what universe does this dude live?

  • @jphvnet

    @jphvnet

    2 жыл бұрын

    If you are capable of refactoring in a good way, new hardware will be cheaper 😉

  • @lepidoptera9337

    @lepidoptera9337

    2 жыл бұрын

    @@jphvnet Refactoring has nothing to do with performance. Are you sure you understand what a compiler does?

  • @MrSkinkarde
    @MrSkinkarde2 жыл бұрын

    Slowest language ever

  • @kaushik853

    @kaushik853

    2 жыл бұрын

    Better than the Monster lik Java

  • @DrBPhD
    @DrBPhD5 жыл бұрын

    Python is incompatible with sustainability. The language is in fact so highly (energy) inefficient that it should be banned. It's effect on performance directly relates to energy demands higher by 2 orders of magnitude (100 times) when compared to codes written in C for instance. Python's CO2 fingerprint is therefore of unacceptable magnitude. Most interpreted languages suffer from similar shortcomings by the way, in contrast to compiled languages.

  • @WerexZenok

    @WerexZenok

    4 жыл бұрын

    That's a short sighted view. With python you have less time training people to code and also less time building your program.

  • @DrBPhD

    @DrBPhD

    4 жыл бұрын

    @@WerexZenok Training people to code and building programs probably (even likely) has a much lower energy fingerprint than actually running the programs and applications (over and over again). The energy-efficiency of computer languages will be of increasing importance in the future. Python scores unacceptably low in this respect.

  • @WerexZenok

    @WerexZenok

    4 жыл бұрын

    @@DrBPhD Well, i'm medic and i'm building AI projects with Python. That wouldn't be possible with C, because I would need to lose too much time learning about the language and fixing small syntax errors instead of focussing on the project itself. I already did a research project using a pyautogui. I started with C, but turned out to be impossible to proceed in a reasonable velocity using C. I changed python and managed to do it in time. If i want velocity in any part of my code i can change to C just in that part. In other words: Python - velocity to build the program. C - velocity to run the progam.

  • @carlossegura403

    @carlossegura403

    3 жыл бұрын

    Not saying you are wrong but did you forget, including “time” into your optimization paradigm? By “time,” I mean the average lifespan of human life on this planet, around 79 years. Beyond anything, time is the most limited resource, as it cannot be created, thus making it the most valuable. Every product and service, including computers and software, exists only for one reason - to save us time! With the proofs mentioned above, we can understand why Python is popular and why your statement is false concerning “energy and sustainability.” I use python because it lets me focus on the problem more than any other language has, and it is the reason it's perfect for data science. Lastly, languages like C, C++ are also important and I cannot discard them as less important to Python. No one is better than the other; we must learn to appreciate what both worlds contribute towards software development and engineering.

  • @DrBPhD

    @DrBPhD

    3 жыл бұрын

    @@carlossegura403 Every product and service, including computers and software, exists only for one reason - to save us time! That is a misconception. Every good product and service exists for one sole purpose: to increase the quality of life. And if I really want to save time I write and run Fortran, which is still the language for large-scale scientific computing par excellence. In fact, the only reason Fortran survived until today IS time and speed. Python on the other hand is grotesquely inefficient. And with a substantial percentage of our energy consumption already being accounted for by information-technologies, sustainability-issues are at the heart of the problems connected with time and energy-inefficient languages. Also, if you master a programming language properly it always lets you focus entirely on the problem itself. No need for Python in that respect, there's a multitude of alternatives which give you speedy code AND ease of use.