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

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
beno
I will often find my self writing things similar to: case some_value do nil -> something() "" -> something() _ -> somethi...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement