Bash array iteration direction and performance

风格不统一 提交于 2019-12-06 12:35:55

问题


Could someone explain what is the cause of the severe slowdown when iterating bash arrays backwards?

Example:

time bash -c 'arr=();for i in {1..100000}; do arr+=( $i );done; echo "Straight"; i=0;while (( $i < 100000 )); do current_element=${arr[$i]}; ((i++));done'

Straight

real    0m0.270s
user    0m0.269s
sys 0m0.002s

time bash -c 'arr=();for i in {1..100000}; do arr+=( $i );done; echo "Reverse"; i=99999;while (( $i > 0 )); do current_element=${arr[$i]}; ((i--));done'

Reverse

real    0m25.569s
user    0m25.589s
sys 0m0.008s

Also

${arr[i-1]} + ${arr[i]} 

is much faster than

${arr[i]} + ${arr[i-1]}

Thanks for your time.

Edit:

bash --version

GNU bash, version 4.3.42(1)-release (x86_64-redhat-linux-gnu)


回答1:


Found some info on the matter.

According to http://www.tldp.org/LDP/abs/html/arrays.html

Arrays in Bash are (circularly) linked lists of type string (char *).

I guess this means that the passed elements are sought from the beginning of the array each time, hence the slowdown. (eg: if we are at i, in order to get to i-1, we should start looking from 0)

Also found a related post with some more info on the matter: http://spencertipping.com/posts/2013.0814.bash-is-irrecoverably-broken.html



来源:https://stackoverflow.com/questions/32592662/bash-array-iteration-direction-and-performance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!