14 ms·
Very cool! I'm curious when it says "did you know that accessing a random element in an array takes linear time", why that's the case, with bash?
by anfractuosity 2y ago
Very cool! I'm curious when it says "did you know that accessing a random element in an array takes linear time", why that's the case, with bash?
- izabera 2y agonormal arrays in bash are implemented as linked lists. bash stores a pointer to the last accessed element, which turns the most common case of iteration into O(1), but the performance is terrible if you need to jump around see some basic benchmarks here https://gist.github.com/izabera/16a46ed79c2248349a1fb83844686df3 https://gist.github.com/izabera/16a46ed79c2248349a1fb8384468... there are also associative arrays which are bucketed hash tables, which are fine for string keys but imho they are hardly ever worth it as a replacement for indexed arrays
- anfractuosity 2y agoThanks a lot, that's very interesting re. it using linked lists, and nice benchmark!