0

I create a list of 24 floats that is needed in a calling function. The calling function will not need to alter the floats, so it can work with a tuple. Is it computationally faster to change the list to a tuple, tuple(list), before returning it to the calling function rather than passing and then using the list the entire time?

A corollary to this is: Should I change a list to a tuple within a function if the function can work with the tuple? I have many instances of creating a list, then using it later in the same function where a tuple of the list would work.

I have several instances of this in my program, so any speed advantage would be helpful to overall performance.

I don't know how to time these things and cannot find a past similar question. I know tuples are more about 3 times faster.

Code sample not needed.

Geza Kerecsenyi
  • 1,127
  • 10
  • 27
creeser
  • 363
  • 1
  • 4
  • 11
  • 4
    Well, python has a module calked `timeit` that can give the answer to your question. My guess is that the conversion will eat up the advantage. – Klaus D. Jul 28 '19 at 20:15
  • can you show some code ? it would be better to be able to answer you quickly – basilisk Jul 28 '19 at 20:26
  • 1
    I would say that it depends entirely on the calling code. If you index the tuple 1 million times in the calling code it might provide you a decent time saving, but if you index it once then it almost certainly wont. – Rob Streeting Jul 28 '19 at 20:29
  • Why would it be quicker to access a `tuple` rather than a `list`? (It might be, I just don't see why...) – JohanL Jul 28 '19 at 20:35
  • 1
    @JohanL tuple use less memory and faster. take a look here https://stackoverflow.com/questions/3340539/why-is-tuple-faster-than-list-in-python – basilisk Jul 28 '19 at 20:44
  • 1
    @basilisk That question only looks at creation of `tuple`s vs `list`s, not accessing the elements, once it is constructed. – JohanL Jul 28 '19 at 20:50
  • @JohanL exactly, why declaring a list if a tuple do the job ? it is a good practice to use tuple instead of lists when no dynamic is needed. – basilisk Jul 28 '19 at 20:52
  • @basilisk Thus, it is not relevant for accessing the elements, since the `list` is already created. Turning it into a `tuple` would add an extra construction and I still don't see what the gain would be. The gain must also overcome this extra cost of creation a new `tuple`. However, if you have the posibility to choose at the time of creation, it is another question altogether. Then it is a matter of whether you will need to update the data at a later stage. – JohanL Jul 28 '19 at 20:58
  • yes that's right. I thought the question was whether to change the declaration to tuple instead of list and not creating a list and then convert it to tuple. – basilisk Jul 28 '19 at 21:02

1 Answers1

-1

After the creation it will not matter anymore so if you want to improve your code than think before the creation if you need a tuple or a list.be aware that tuple are fixed size and list are dynamic so it ll depend on what you are trying to do. after creation it doesnt actually matter because accessing elements is not faster or anything and it doesnt make sense to convert a list to tuple after creation that will not make your code faster or more efficient. you can look here for more link

However to test the execution Time you can use the timeit module

import timeit

start = timeit.default_timer()
# your function or piece of code
end= timeit.default_timer()
print(end-start)
basilisk
  • 1,156
  • 1
  • 14
  • 34
  • Another way of asking this question: Where should the overhead of converting a list to a tuple occur; before returning the call, or afterwards? There has to be some extra overhead in python during the return between a tuple and a list. This requires looking under the hood at how python handles the return of a tuple/list. One would think there would be more overhead with a list because it has more functionality, but maybe it only requires pointers and some sort of identifier.... – creeser Jul 29 '19 at 23:42