●リストの結合にかかる時間-2
前回の“リストの結合にかかる時間-1”で予測に反して時間がかかり過ぎる原因が分かりました。結合する際に(リストの最初から終わりから2番目)+(新たなデータ)+(最後のリスト)というように要素数が偏っていたのが原因だったようです。今回は真ん中で分けて結合してみました。
本来はランダムな位置で結合すれば良かったのですが、そういった処理も時間計測の中に入ってしまうのでやめました。(前回と今回の間位が実質かかる時間ということで)
こうして見ると、前々回紹介したソートのハンドラで5000項目のソートにかかる時間のほとんどはこの部分の処理だったということになります。
また、一回当たりの結合にかかる時間はリストの総数に比例しているであろうことが見て取れます。
前回と今回の比較で要素の範囲指定で計測時間が変わってくることもわかっているので、範囲指定での参照に時間がかかるのか、それとも結合に時間がかかるのかを調べておく必要があるようです。
set theRec to {aList:{}}
set theRecRef to a reference to theRec
set aItem to 10
set timeList to {}
set T to current date
repeat 10 times --こちらは時間計測のため10回繰り返す
set aList of theRecRef to {500, 500}
set N to 2
repeat 1000 - 2 times
set i to N div 2
set aList of theRecRef to items 1 thru i of aList of theRecRef & {aItem} & items (i + 1) thru -1 of aList of theRecRef
set N to N + 1
end repeat
end repeat
set end of timeList to (current date) - T
set T to current date
repeat 1 times
set aList of theRecRef to {500, 500}
set N to 2
repeat 5000 - 2 times
set i to N div 2
set aList of theRecRef to items 1 thru i of aList of theRecRef & {aItem} & items (i + 1) thru -1 of aList of theRecRef
set N to N + 1
end repeat
end repeat
set end of timeList to (current date) - T
get timeList
結果 {5, 14}