view Looping.cpp @ 4:d788c88f41bd >>178

Rubyのベンチマークを追加。ソースコードは2ch.netの>>178からコピー。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Thu, 17 Feb 2011 09:01:50 +0900
parents 2216535ade9c
children
line wrap: on
line source

#include <stdio.h>
#include <limits.h>
#include <sys/time.h>

class Looping {
public:
	Looping();
	int calc(int n);
private :
	int n0;
};

Looping::Looping(){
	n0 = 0;
}

int Looping::calc(int n){
	int n1 = n0 + (1 - 2*(n%2));
	n0 = n;
	return n1;
}

double gettimeofday_sec()
{
  struct timeval tv;
  gettimeofday(&tv, NULL);
  return tv.tv_sec + (double)tv.tv_usec*1e-6;
}

int main(){
  Looping *o = new Looping();
  unsigned c;
  int n = 1;
  double t1, t2;
  t1 = gettimeofday_sec();
  for(c=0;c<INT_MAX;c++){
    n = o->calc(n);
  }
  t2 = gettimeofday_sec();
  printf("%d\nC++\t%f\n", n, t2 - t1);
  delete o;
  return 0;
}