belteconti
A faster alternative to Enum.at
Is there a faster alternative to Enum.at? Basically, something in O(1)
Most Liked
al2o3cr
There are many, but which one to use is strongly-coupled to what operations you need to be efficient:
-
if you want sparse data (most indexes don’t have anything in them), you could use a
Mapofindex→value. This wastes space with keys, but hasO(log(n))access for more than 32 elements. -
if you have arrays of fixed size (not too large), then tuples are very fast (
elemis constant-time) to read. Updating is expensive, and resizing is as well - both copy every pointer in the structure. -
if you’re converting an algorithm that uses zero-based indexing into a block of memory to simulate some other data structure (ie, a queue / a stack / a circular list), a better data structure may help. For instance, the Erlang
:queuemodule allows amortized-constant-time pushing and popping from either end.







