changeset 5:861b93741d60

id:sumim氏のSmalltalk版とRuby版のコードを追加。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Thu, 17 Feb 2011 11:32:17 +0900
parents 57283e4fdd4b
children 5e09f7cf87f5
files Looping.rb Looping.st
diffstat 2 files changed, 57 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.rb	Thu Feb 17 11:32:17 2011 +0900
@@ -0,0 +1,22 @@
+#!/usr/bin/env ruby
+class Looping
+ def initialize
+    @n0 = 0
+ end
+
+ def calc(n)
+    n1 = @n0 + (1 - 2*(n%2))
+    @n0 = n
+    return n1
+ end
+end
+
+l = Looping.new
+n = 1
+t1 = Time.now
+2147483647.times{ |c|
+ n = l.calc(n)
+}
+t2 = Time.now
+print "#{n}\nRuby\t#{t2 - t1}\n"
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.st	Thu Feb 17 11:32:17 2011 +0900
@@ -0,0 +1,35 @@
+'From Squeak4.2 of 4 February 2011 [latest update: #10966] on 17 February 2011 at 8:44:09 am'!
+Object subclass: #Looping
+	instanceVariableNames: 'n0'
+	classVariableNames: ''
+	poolDictionaries: ''
+	category: 'Benchmark-Looping'!
+
+!Looping methodsFor: 'calculation' stamp: 'sumim 2/17/2011 00:17'!
+calc: n
+	| n1 |
+	n1 := n0 + (1 - (2 * (n \\ 2))).
+	n0 := n.
+	^n1! !
+
+!Looping methodsFor: 'initializing' stamp: 'sumim 2/16/2011 23:59'!
+initialize
+	n0 := 0! !
+
+
+!Looping class methodsFor: 'benchmark' stamp: 'sumim 2/17/2011 00:52'!
+benchmark
+	"self benchmark"
+	| l n t1 t2 |
+	l := Looping new.
+	n := 1.
+	t1 := Time millisecondClockValue.
+	2147483647 timesRepeat: [
+		n := l calc: n
+	].
+	t2 := Time millisecondClockValue.
+	Transcript cr; show: n printString.
+	Transcript cr; show: 'Smalltalk'; tab; show: (t2 - t1 / 1000.0) printString! !
+
+Looping benchmark!
+