访问数组元素时的性能考虑因素

时间:2015-11-27 11:40:20

标签: bash

我目前正在空闲时间学习一些bash,并使用bash进行一些简单的编码挑战。解决最后一个挑战我发现了一些奇怪的优化问题:

# read the number of input values
read N

# read all input values into an array
readarray -n $N P

# sort the input array in an ascending order
PS=($(printf "%s\n" ${P[@]} | sort -n))

# init the current minimum distance of two horses by the max input value plus one
Min=10000001

# iterate over the ascending sorted array
# and find the minimum distance
for ((i = 1; i < N; i++)); do
  # compute the difference between the current and the last
  #D=$((PS[i]-PS[i-1]))
  D=$((-PS[i-1]+PS[i]))

  if [ $D -le $Min ]; then
    Min=$D
  fi
done

# finally print the minimum distnce
echo $Min

以某种方式访问​​PS [i]并且之后PS [i-1]导致100'000输入值的测试用例耗尽时间。然而,以相反的顺序访问完全相同的数组元素会导致测试用例正确运行。非关联数组访问应该花费O(1)时间,那么访问顺序如何影响运行时性能呢?内部是否有一些bash魔法我不知道(比如数组被实现为单链表或类似的东西?)

1 个答案:

答案 0 :(得分:0)

Bash数组不是C语义中的​​数组,因为你可以拥有稀疏数组而不浪费内存:

a=([1]=0 [100000000]=2 [10000000000]=3)

正常数组允许O(1)访问,因为索引指定的内存位置可以通过O(1)中的公式计算,这通常是因为数组存储在连续内存中而您只需要计算一个偏移。通常,稀疏数组是使用链表实现的,即使它们是使用像hashmaps这样的其他东西实现的,access may not be O(1)