Bash数组迭代方向和性能 [英] Bash array iteration direction and performance

查看:111
本文介绍了Bash数组迭代方向和性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以解释在向后迭代bash数组时导致严重减速的原因是什么?

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

示例:

time bash -c'arl =(); for {1..100000}; do arr + =($ i);完成;回声直; i = 0;而(($ i <100000)); do current_element = $ {arr [$ i]}; ((i ++));完成'

Straight

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

time bash -c'arl =(); for {1..100000}; do arr + =($ i);完成;回声反向; i = 99999;而(($ i> 0)); do current_element = $ {arr [$ i]}; ((i - ));完成'

Reverse

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

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

快于

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

感谢您的时间。

编辑:


bash --version

bash --version

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

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


推荐答案

找到有关此事的一些信息。

Found some info on the matter.

根据 http://www.tldp.org/LDP/abs/html/arrays.html


Bash中的数组是(循环)链接的字符串类型(char *)。

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

我想这意味着要传递传递的元素每次数组的开头,因此减速。 (例如:如果我们在i,为了到达i-1,我们应该从0开始查看)

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)

还找到了一个相关帖子,其中包含更多信息问题:
http://spencertipping.com/posts/ 2013.0814.bash-is-irrecoverably-broken.html

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

这篇关于Bash数组迭代方向和性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆