view Looping.cpp @ 0:2216535ade9c

メンバー関数/メソッド呼び出しのベンチマーク。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Tue, 15 Feb 2011 13:47:19 +0900
parents
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;
}