view Looping.go @ 12:dab1e83799e4 >>178 tip

R版のソースコードを追加。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Sat, 04 Feb 2012 04:10:01 +0900
parents 308cc64c24b9
children
line wrap: on
line source

package main

import fmt "fmt"
import tm "time"

type Looping struct {
	n0 int;
}

func (this *Looping) calc(n int) (int) {
	var n1 = this.n0 + (1 - 2*(n%2))
	this.n0 = n
	return n1
}

func NewLooping () *Looping {
	var o = new(Looping)
	o.n0 = 0
	return o
}

func main() {
	var o = NewLooping()
	var n = 1
	var t1 = tm.Nanoseconds()
	for c:=0; c<2147483647; c++ {
		n = o.calc(n)
	}
	var t2 = tm.Nanoseconds()
	fmt.Printf("%d\nGo:\t%f\n", n, ((float64)(t2 - t1))/1000000000)
}