comparison Looping.c @ 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 <stdlib.h>
3 #include <limits.h>
4 #include <sys/time.h>
5
6 typedef struct {
7 int n0;
8 } Looping;
9
10 int calc(Looping *s, int n){
11 int n1 = s->n0 + (1 - 2*(n%2));
12 s->n0 = n;
13 return n1;
14 }
15
16 double gettimeofday_sec()
17 {
18 struct timeval tv;
19 gettimeofday(&tv, NULL);
20 return tv.tv_sec + (double)tv.tv_usec*1e-6;
21 }
22
23 int main(){
24 Looping *o;
25 unsigned c;
26 int n = 1;
27 double t1, t2;
28
29 o = (Looping *)malloc(sizeof(Looping));
30 o->n0 = 0;
31
32 t1 = gettimeofday_sec();
33 for(c=0;c<INT_MAX;c++){
34 n = calc(o, n);
35 }
36 t2 = gettimeofday_sec();
37 printf("%d\nC\t%f\n", n, t2 - t1);
38
39 free(o);
40 return 0;
41 }