changeset 12:dab1e83799e4 >>178 tip

R版のソースコードを追加。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Sat, 04 Feb 2012 04:10:01 +0900
parents b952a5d72c9f
children
files Looping.R
diffstat 1 files changed, 28 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.R	Sat Feb 04 04:10:01 2012 +0900
@@ -0,0 +1,28 @@
+# クラス定義
+setClass("Looping", contains = "numeric")
+# 総称関数を宣言
+setGeneric("calc", function(obj, n){
+	standardGeneric("calc")
+})
+# メソッドを追加
+setMethod("calc", signature="Looping", definition=function(obj, n){
+	n1 <- obj@.Data + (1 - 2*(n%%2))
+	obj@.Data <- n1
+	return(n1)
+})
+
+# インスタンスを作成
+l <- new("Looping")
+c <- 0
+n <- 1
+t1 <- proc.time()
+# 本当は2147483647でベンチマークすべき
+repeat {
+	if(c>=2147483)
+		break
+	n <- calc(l, n)
+	c = c + 1
+}
+t2 <- proc.time()
+print(paste(" R", t2["elapsed"] - t1["elapsed"]))
+print(paste(" R", (t2["elapsed"] - t1["elapsed"])*2147483647/2147483, "[Estimate]"))