Mailing List Archive

why is a search thru a Tuple slower ? ---- (meaningless indentations)
is Comp.Lang.Python very active????


why is a linear search thru a Tuple slower
(than thru a (corresponding) List ) ???


sometimes, i 'd like to put meaningless indentations
like i do (twice) below
( how can it do this ?)

_______________________________

import time

X= [x for x in range(10000) ]
TupX= tuple(X)
SetX= set(X)

Items=[20, 30, 100, 200, 1000, 5000, 7000, 8000, 9000 ]
Count=1000

Time = time.time()
for _ in range(Count):
for i in X: test= i*i; test= i*i *i
print( ' \t ({0:.4f}) X'.format(time.time()-Time) )

Time = time.time()
for _ in range(Count):
for i in TupX: test= i*i; test= i*i *i
print( ' \t ({0:.4f}) TupX'.format(time.time()-Time) )



Time = time.time()
for _ in range(Count):
for i in Items: test= i in X
print( ' \t ({0:.4f}) i in X'.format(time.time()-Time) )

Time = time.time()
for _ in range(Count):
for i in Items: test= i in TupX
print( ' \t ({0:.4f}) i in TupX'.format(time.time()-Time) )

--
https://mail.python.org/mailman/listinfo/python-list
Re: why is a search thru a Tuple slower ? ---- (meaningless indentations) [ In reply to ]
On 2/20/23 18:01, Hen Hanna wrote:
> is Comp.Lang.Python very active????
Fairly. Apparently the cool kids are using the Python Discourse forum.

> why is a linear search thru a Tuple slower
> (than thru a (corresponding) List ) ???

I cannot say, unfortunately. Perhaps doing some analysis of the byte
code with the disasm module could tell you what the interpreter is doing
and why it is slower. Since tuples are read only, I cannot think of any
reason to use them for large, generated structures. A list is far better
in my opinion. I use tuples mainly for bundling small amounts of
information together, such as coordinates, or returning multiple values
from a function.

> sometimes, i 'd like to put meaningless indentations
> like i do (twice) below
> ( how can it do this ?)

Fortunately you cannot. Such indents are syntax errors.

And I have to say it makes your emails very hard to read and understand
when you indent your sentences as you do. Looks poetic but hard to read.

Also your python example code was not run-able either thanks to those
two extra indents which are syntax errors. It's always helpful to post
complete and working code examples when asking for help or wanting to
get discussion on a piece of code.
--
https://mail.python.org/mailman/listinfo/python-list