+ 1

How does simple use of curly braces and parentheses make a difference in speed?

I read that types are faster than lists. I want to know know how and why? is it because the code for list function written for python is lengthy as it allows mutation?

14th Feb 2017, 6:54 AM
Yehlo in
Yehlo in - avatar
2 odpowiedzi
+ 3
Tuples use parenthesis and they are technically a little faster than lists as they are immutable and don't need any tools for manipulating data within them. Sets use curly braces, are unordered and are based on a binary tree structure. That means that checking existance of some element in the set takes O(log2(n)) time, where n is set's size, versus list's O(n) existance check time. Log2(n) is a number that when 2 is raised to its power it gives n. To understand how faster it actually is, imagine a list and a set, both containing 1000000 items. In list, you need to seek through it until you either find your element or fail to do it, so here come plain 1000000 operations (a little less actually on average, but that number is still directly proportional to n). In set, elements are organised in a complicated pattern. Just think of a tree where first row contains 1 element and each next contains roughly double of the previous (2, 4, 8, etc). It happens so that if you want to add an element, or delete an element, or check existance, you need to go down and down, but checking only ONE element on each row. And that takes us to fascinating 20 operations! That's so, 1000000 items only take about 20 rows!
14th Feb 2017, 1:17 PM
DotJason
0
curly braces are use for sets. If you use a lot of "in"s in your code over that collection, it is way faster to use a set than a list
14th Feb 2017, 10:38 AM
Amaras A
Amaras A - avatar