●リスト参照にかかる時間-3
今回は a reference to を使って“参照にかかる時間-1”と同じ実験をしてみました。時間計測にあたっては、ずいぶん高速になったこともあり、前回は20回繰り返した所を今回は100回繰り返しています。結果は何度も参照する値に変数名を付けた場合が6秒、毎回参照した場合が10秒、前回に比べ約8倍高速になりました。また、やはり何度も使う値には変数名を付けた方が速いという結果が出ました。
set N to {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
set numList to {}
repeat 1000 times
set end of numList to (some item of N) * 100 + (some item of N) * 10 + (some item of N)
end repeat
set numListRef to a reference to numList --参照クラスに変換する
set timeList to {}
--何度も参照する値にはあらかじめ変数名を付けた場合
set T to current date
repeat 100 times --実験データを得るために100回繰り返す
set A to 0
set theItem to item 1 of numListRef --あらかじめ変数名を付けておく
set i to 2
repeat (count numListRef) - 1 times
if theItem > item i of numListRef then
set A to A + 1
end if
set i to i + 1
end repeat
end repeat
set end of timeList to (current date) - T
--比較する際に毎回参照した場合
set T to current date
repeat 100 times --実験データを得るために100回繰り返す
set A to 0
set i to 2
repeat (count numListRef) - 1 times
if item 1 of numListRef > item i of numListRef then
set A to A + 1
end if
set i to i + 1
end repeat
end repeat
set end of timeList to (current date) - T
get timeList
結果{6, 10}