+ 1
Which is faster?
x = [1,2,3] x.pop() or x = (1,2,3) x = x[:2]
4 Réponses
0
We can do a simple experiment on this.
https://code.sololearn.com/cchelRgdWPo4/?ref=app
Averagely speaking, it seems that your second code runs faster than the first one, but they both run very fast (each takes less than 1 ms), so you can choose the code you like, and get similar performance.
+ 4
Why you comparing for the fastness. Both perform different operations..
In first, you removing last element jus from a list.
In 2nd, you are copying 0 to last element into a variable from a tuple. It is more operations than 1st.
Since there is just 3 elements no difference. But I think not comparable for fastness..
+ 1
Thanks
+ 1
Hello World
Good thinking.. I think those are 2 different operations but checked with time differences the pop operation taking more nano milliseconds than 2nd way slicing...
Hope this helps...
&
You're welcome...