2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

シミュレーションの実行速度を上げるには

Posted at

参考リンク
https://qiita.com/ShiraTakiSuki/items/87bcba546e063c16d846

結論

結果を保持するときに,append() や rbind() を使わない。

以下のようにすると,実行時間が馬鹿にならない。
回数が多くなると,フリーズしたようになる(ループ回数を10倍したのに,10倍以上(無限に?)時間がかかる)。

result <- numeric()
A <- numeric() # 一応データ保存
for (j in 1:M) {
	result <- append(result, c == W)
    A <- rbind(A, a)
}

append() や rbind() する回数はたいてい for ループに入る前にわかっている(わかっていないなら,わかるようにする)なら,結果を保持するメモリーを前もって確保しておき, for ループ内では添字を使って保存する。

そうすれば,実行速度は 10 倍くらいになる(はず)。フリーズ状態にもならない。

result <- numeric(M)
A <- matrix(0, M, N) # 一応データ保存
for (j in 1:M) {
	result[j] <- c == W
	A[j,] <- a
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?