Mercurial > op > Looping
comparison Looping.M @ 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 #import <stdio.h> | |
2 #import <objc/Object.h> | |
3 #import <limits.h> | |
4 #import <sys/time.h> | |
5 | |
6 @interface Looping : Object { | |
7 int n0; | |
8 } | |
9 - (id)init; | |
10 - (int)calc:(int)n; | |
11 @end | |
12 | |
13 @implementation Looping | |
14 - (id)init{ | |
15 [super init]; | |
16 n0 = 0; | |
17 return self; | |
18 } | |
19 - (int)calc:(int)n { | |
20 int n1 = n0 + (1 - 2*(n%2)); | |
21 n0 = n; | |
22 return n1; | |
23 } | |
24 @end | |
25 | |
26 double gettimeofday_sec() | |
27 { | |
28 struct timeval tv; | |
29 gettimeofday(&tv, NULL); | |
30 return tv.tv_sec + (double)tv.tv_usec*1e-6; | |
31 } | |
32 | |
33 int main(){ | |
34 id o = [[Looping alloc] init]; | |
35 unsigned c; | |
36 int n = 1; | |
37 double t1, t2; | |
38 t1 = gettimeofday_sec(); | |
39 for(c=0;c<INT_MAX;c++){ | |
40 n = [o calc:n]; | |
41 } | |
42 t2 = gettimeofday_sec(); | |
43 printf("%d\nObjective-C\t%f\n", n, t2 - t1); | |
44 return 0; | |
45 } |