Trolleger

Trolleger

How does List vs Tuple preformance work

I heard that editing a tuple is a much more expensive operation then editing a list

i just don’t get the why, when a list is mutated a new list made in memory when edited, same as tuple; they use references from other variables and only really store the new values so it should both be fast. “the elements themselves are not copied. When you update a tuple, all entries are shared between the old and the new tuple, except for the entry that has been replaced” So I don’t even understand considering that previous statement the whole part about “a whole new tuple made in memory” it seems contradicting

And what does it matter if it’s contiguous If they are both in memory, isn’t basically every value like that, (Which is if I got the definition right just one place in memory, not fragmented)? Maybe not lists because of the whole linked thing but that is just a wild throw of a guess. I really don’t know :confused: I’m new to this stuff

So what actually is the difference with performance (considering all the factors mentioned before and others)? Thanks!

Most Liked

LostKobrakai

LostKobrakai

I heard that editing a tuple is a much more expensive operation then editing a list

Are you sure you heard it that way and not the reverse?

Lists have very different performance characteristics depending on which section of the list you edit. Editing in the front is fine. Editing in the back a lot less so, given it’ll invalidate all preceeding cons cells. The values fill be fine, but the refences to the next cell need to be adjusted all the way from the front to the edited element.

Zurga

Zurga

I did a small benchmark that compares functions that exists for lists and for tuples. The source is here: Benchmarks of tuples vs lists · GitHub

Size is 100000 items.

Name                                           ips        average  deviation         median         99th %
list insert_at_start                      799.46 K        1.25 μs    ±56.90%        1.40 μs        1.47 μs
list delete_at_start                      763.82 K        1.31 μs  ±2526.28%        1.40 μs        1.47 μs
tuple delete_at_end                         6.54 K      152.85 μs    ±58.71%      118.17 μs      347.78 μs
tuple delete_at_start                       6.50 K      153.88 μs    ±57.49%      117.34 μs      352.52 μs
tuple delete_at_middle                      6.42 K      155.84 μs    ±58.16%      118.31 μs      381.88 μs
tuple delete_at_random_index                6.31 K      158.49 μs    ±59.24%      119.15 μs      411.01 μs
list insert_at_random_index                 6.21 K      161.13 μs    ±24.39%      156.79 μs      317.22 μs
list delete_at_random_index                 6.19 K      161.51 μs    ±18.80%      169.64 μs      197.02 μs
tuple insert_at_middle                      5.34 K      187.22 μs    ±71.53%      127.88 μs      660.99 μs
tuple insert_at_random_index                5.33 K      187.77 μs    ±70.21%      127.88 μs      659.84 μs
tuple insert_at_start                       5.33 K      187.77 μs    ±74.48%      123.48 μs      697.20 μs
tuple insert_at_end                         5.22 K      191.57 μs    ±73.29%      130.33 μs      735.98 μs
list insert_at_middle                       4.08 K      245.29 μs    ±27.99%      223.98 μs      492.32 μs
list sum                                    3.83 K      261.43 μs     ±8.54%      250.45 μs      315.83 μs
list to_tuple                               3.50 K      286.10 μs    ±46.85%      224.19 μs      701.57 μs
list product                                3.31 K      302.34 μs    ±20.24%      338.18 μs      353.47 μs
tuple random_replace                        2.59 K      386.77 μs    ±56.91%      407.75 μs     1144.39 μs
list delete_at_middle                       1.71 K      583.73 μs    ±30.48%      701.44 μs      920.59 μs
tuple sum                                   1.47 K      679.29 μs    ±51.47%      598.77 μs     2507.16 μs
tuple product                               1.44 K      694.17 μs    ±55.88%      548.48 μs     2409.10 μs
tuple to_list product                       1.24 K      807.42 μs    ±65.46%      511.67 μs     2191.66 μs
tuple to_list sum                           1.20 K      832.56 μs    ±70.15%      482.20 μs     2213.17 μs
list insert_at_end                          0.94 K     1061.78 μs    ±37.81%     1263.79 μs     1806.01 μs
tuple to_list                               0.78 K     1287.32 μs    ±79.96%      906.29 μs     4748.49 μs
list random_replace                         0.67 K     1496.43 μs    ±84.24%     1340.76 μs     6404.22 μs
list delete_at_end                          0.52 K     1920.07 μs    ±53.00%     1879.47 μs     4942.26 μs
list grow_prepend                           0.39 K     2538.98 μs    ±51.59%     2451.91 μs     6270.11 μs
list to_tuple random_replace to_list        0.27 K     3737.02 μs    ±27.80%     3603.24 μs     6261.40 μs
tuple grow_append                        0.00007 K 13584333.49 μs     ±0.00% 13584333.49 μs 13584333.49 μs
tuple grow_prepend                       0.00006 K 16420455.62 μs     ±0.00% 16420455.62 μs 16420455.62 μs
list grow_append concat                  0.00004 K 22971996.78 μs     ±0.00% 22971996.78 μs 22971996.78 μs
list grow_append                         0.00004 K 23192857.58 μs     ±0.00% 23192857.58 μs 23192857.58 μs

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" => #BSON.ObjectId<58eb1a7a9ad169198c3dXXXX>, "email" => ...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49134 226
New
AstonJ
Posting this to see if we can make things easier for people to get into Neovim. If you use Neovim and have a favourite distro please let ...
New

We're in Beta

About us Mission Statement