Last active
March 23, 2016 11:12
-
-
Save vain0x/0d9cf5591bf5b27bb295 to your computer and use it in GitHub Desktop.
配列拡張の速度のベンチマーク
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 配列の自動拡張の計測 | |
// @require "benchmark.hsp" <https://gist.github.com/vain0/5aeda019eb11835987f4> | |
#define global total_size (50000) | |
#define global cnt_trial (5) | |
logmes "生成する配列の長さ = " + total_size | |
logmes "各テストの試行回数 = " + cnt_trial | |
assert cnt_trial >= 3 | |
#include "util/benchmark.hsp" | |
mes "str 型" | |
assert | |
lb = *LTestStr0, *LTestStr1, *LTestStr2 | |
msg = "事前確保", "手動拡張", "自動拡張" | |
repeat 3 | |
benchmark_logmes lb(cnt), "str " + msg(cnt), cnt_trial | |
dim xs | |
loop | |
mes "int 型" | |
assert | |
lb = *LTestInt0, *LTestInt1, *LTestInt2 | |
msg = "事前確保", "手動拡張", "自動拡張" | |
repeat 3 | |
benchmark_logmes lb(cnt), "int " + msg(cnt), cnt_trial | |
dim xs | |
loop | |
mes "double 型" | |
assert | |
lb = *LTestDouble0, *LTestDouble1, *LTestDouble2 | |
//msg =... | |
repeat 3 | |
benchmark_logmes lb(cnt), "double " + msg(cnt), cnt_trial | |
dim xs | |
loop | |
mes "newmod" | |
assert | |
lb = *LTestNewmod0, *LTestNewmod1 | |
msg = "事前確保" , "自動拡張" | |
repeat 2 | |
benchmark_logmes lb(cnt), "newmod " + msg(cnt), cnt_trial | |
dim vs | |
loop | |
stop | |
//----------------------------------------------------------------------------- | |
// int 型のテスト | |
*LTestInt0 | |
/** | |
必要な要素数を最初に確保 | |
利点: 効率的に無駄がない。 | |
欠点: 事前に必要な要素数が分かるときにしか使えない。 | |
//*/ | |
dim xs, total_size | |
repeat total_size | |
xs(cnt) = cnt | |
loop | |
return | |
*LTestInt1 | |
/** | |
手動確保 | |
要素数が足りないときに、要素数を2倍に増やす。 | |
利点: | |
事前確保より2倍程度の遅いが、十分高速 | |
欠点: | |
コードがめんどくさい。 | |
length が役に立たなくなる。 | |
メモリを最大2倍使用する。 | |
//*/ | |
dim xs | |
repeat total_size | |
// 要素数が足りなければ2倍に伸ばす | |
if ( length(xs) <= cnt + 1 ) { | |
xs((cnt + 1) * 2) = 0 | |
} | |
xs(cnt) = cnt | |
loop | |
return | |
*LTestInt2 | |
/** | |
自動拡張 | |
利点: | |
短い。(何も書かなくていい。) | |
欠点: | |
遅い。 | |
HSP3.5b2現在の実装では、(size > 64 のとき) 毎回動的確保とデータコピーを行う。 | |
倍々で確保することにすれば計算量は「手動拡張」と同じになる。 | |
毎回 length の値を調整するために自動拡張のための関数が呼ばれるので遅いが、十分高速。 | |
ただしメモリ使用量は最大2倍になる。 | |
//*/ | |
dim xs | |
repeat total_size | |
xs(cnt) = cnt | |
loop | |
return | |
//----------------------------------------------------------------------------- | |
// str 型の試験 | |
*LTestStr0 | |
// 事前確保 | |
sdim xs, , total_size | |
repeat total_size | |
xs(cnt) = str(cnt) | |
loop | |
return | |
*LTestStr1 | |
// 手動確保 | |
sdim xs | |
repeat total_size | |
// 要素数が足りなければ2倍に伸ばす | |
if ( length(xs) <= cnt + 1 ) { | |
xs((cnt + 1) * 2) = "" | |
} | |
xs(cnt) = str(cnt) | |
loop | |
return | |
*LTestStr2 | |
// 自動拡張 | |
sdim xs | |
repeat total_size | |
xs(cnt) = str(cnt) | |
loop | |
return | |
//----------------------------------------------------------------------------- | |
// double 型の試験 | |
*LTestDouble0 | |
// 事前確保 | |
ddim xs, total_size | |
repeat total_size | |
xs(cnt) = double(cnt) | |
loop | |
return | |
*LTestDouble1 | |
// 手動確保 | |
ddim xs, 1 | |
repeat total_size | |
// 要素数が足りなければ2倍に伸ばす | |
if ( length(xs) <= cnt + 1 ) { | |
xs((cnt + 1) * 2) = 0.0 | |
} | |
xs(cnt) = double(cnt) | |
loop | |
return | |
*LTestDouble2 | |
// 自動拡張 | |
ddim xs, 1 | |
repeat total_size | |
xs(cnt) = double(cnt) | |
loop | |
return | |
//----------------------------------------------------------------------------- | |
// newmod の試験 | |
#module mcempty x_ | |
#global | |
*LTestNewmod0 | |
// 事前確保 | |
dimtype vs, vartype("struct"), total_size | |
repeat total_size | |
newmod vs, mcempty@ | |
loop | |
return | |
*LTestNewmod1 | |
// 自動拡張 | |
dimtype vs, vartype("struct") | |
repeat total_size | |
newmod vs, mcempty@ | |
loop | |
return |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "util/benchmark.hsp" | |
block = "0123456789ABCDEF" | |
lb = *LTest0, *LTest1, *LTest2 | |
msg = "事前確保", "手動拡張", "自動拡張" | |
repeat 3 | |
benchmark_logmes lb(cnt), "str " + msg(cnt), cnt_trial | |
dim xs | |
loop | |
assert | |
end | |
*LTestStr0 | |
// 事前確保 | |
sdim xs, total_size | |
repeat total_size | |
xs(cnt) += block | |
loop | |
return | |
*LTestStr1 | |
// 手動確保 | |
sdim xs | |
bufsize = 64 | |
repeat total_size | |
// 要素数が足りなければ2倍に伸ばす | |
if ( length(xs) <= cnt + 1 ) { | |
xs((cnt + 1) * 2) = "" | |
} | |
xs(cnt) = str(cnt) | |
loop | |
return | |
*LTestStr2 | |
// 自動拡張 | |
sdim xs | |
repeat total_size | |
xs(cnt) = str(cnt) | |
loop | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
require: benchmark.hsp