Set Speed Test

Minh Pham

Hi James,

Please tell us how to set up the Python Console and test as you do in this Speed Test

Thanks Minh

James Uejio RP Team

Hi @Minh thank you for the response. See ipython.org/install.html for how to install iPython where you can use timeit.

Zarata

The tuple tutorial made a point that tuples are used because they are faster for many operations compared to lists. You’ve proven that the membership operation is “the same” tuple vs. list. Is this detail worth an explanation / rectification?

keyurratanghayra

Hey There,

May I know which IDE, you are using for this lesson? It looks swanky.

Ricky White RP Team

He’s using VS Code :)

James Uejio RP Team

@keyurratanghayra Ricky is correct I am using VS Code! I have a “Dainty – Monokai” theme see dainty.site/vs. Also using iTerm for my terminal.

@Zarata I’m a little confused regarding the question. At around 5:25 you can see membership in sets is 57 microseconds while membership in lists is 5 milliseconds. So membership in sets is 100x faster. Let me know if you are referring to another part of the video!

Zarata

In the Introduction to Python learning path as a whole, there is another module Lists and Tuples. In that module (not this Sets module) the author made the point that tuples are used because they are faster than lists. Your video clocks comparable list and tuple operations as ~5.7 ms both. Your video validates the speed superiority of set implementation, but invalidates that tuple/list statement by the other author. I’m curious if there’s a rectification. Your timing example re-ignites the question I remember from the tuples/list module “why use tuples at all?”

BTW, I recall someone along the way asked “what are sets good for?” I have found set concepts VERY useful in some graph theory numerical analysis I performed using Java, but I didn’t find in Java at the time all of the method and operator support that Python provides. To that person: sets do have real world use and the set concept can make some numerical analysis problems “easier”.

RMS

Awesome, didn’t know about the `%timeit` functionality of ipython. Is that why you’re using ipython instead of ptpython? Or are there any other advantages?

James Uejio RP Team

@zarata Great observation and after some research, the article is correct to an extent. Tuples are only faster than lists when initializing them, not necessarily adding them together (like in the video). For example:

``````>>> %timeit (1, 2, 3)
6.9 ns ± 0.0379 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)
>>> %timeit [1, 2, 3]
46.4 ns ± 0.341 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
``````

In terms of what I showed in the video, you can see lists are slightly faster because for tuples you need to initialize and add together vs just appending to a list. I probably should have named the functions initalize_and_add or something. In the real world however this is pretty negligible and the it’s not necessarily worth worrying about optimizing tuples vs. lists in terms of speed. You can learn more here: stackoverflow.com/questions/3340539/why-is-tuple-faster-than-list-in-python.

However there are many benefits to tuples because they are hashable so you can use them as keys for dictionaries or you can put them in sets. They are also immutable which can lead to cleaner code and thread safe programming. See hackernoon.com/5-benefits-of-immutable-objects-worth-considering-for-your-next-project-f98e7e85b6ac for more!

@RMS I’m not super familiar with pypython but ipython has been pretty easy for me to use. I don’t think the REPL matters too much (sometimes when I’m lazy I’ll just pull up the built in one to double check some syntax), but ipython is easy to install and use so I chose that one. This video course realpython.com/courses/finding-perfect-python-code-editor/ has some more information on choosing a python code editor, which is probably more useful than finding the perfect REPL.

to join the conversation.

Lesson Completed!
Lesson Bookmarked
Request Failed :(