comparison Looping.cpp @ 0:2216535ade9c

メンバー関数/メソッド呼び出しのベンチマーク。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Tue, 15 Feb 2011 13:47:19 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:2216535ade9c
1 #include <stdio.h>
2 #include <limits.h>
3 #include <sys/time.h>
4
5 class Looping {
6 public:
7 Looping();
8 int calc(int n);
9 private :
10 int n0;
11 };
12
13 Looping::Looping(){
14 n0 = 0;
15 }
16
17 int Looping::calc(int n){
18 int n1 = n0 + (1 - 2*(n%2));
19 n0 = n;
20 return n1;
21 }
22
23 double gettimeofday_sec()
24 {
25 struct timeval tv;
26 gettimeofday(&tv, NULL);
27 return tv.tv_sec + (double)tv.tv_usec*1e-6;
28 }
29
30 int main(){
31 Looping *o = new Looping();
32 unsigned c;
33 int n = 1;
34 double t1, t2;
35 t1 = gettimeofday_sec();
36 for(c=0;c<INT_MAX;c++){
37 n = o->calc(n);
38 }
39 t2 = gettimeofday_sec();
40 printf("%d\nC++\t%f\n", n, t2 - t1);
41 delete o;
42 return 0;
43 }