Mercurial > op > Looping
comparison Looping-IMP.M @ 1:9d6fb29e41ce
Objective-C IMPのベンチマークが抜けていたため、追加。
author | "uncorrelated zombie" <uncorrelated@yahoo.co.jp> |
---|---|
date | Tue, 15 Feb 2011 18:38:19 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
0:2216535ade9c | 1:9d6fb29e41ce |
---|---|
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 int (*calc)(id,SEL,int); | |
36 SEL selector = @selector(calc:); | |
37 /* calc = (int (*)(id,SEL,int))objc_msg_lookup(o, selector); */ | |
38 calc = (int (*)(id,SEL,int))[Looping instanceMethodFor:selector]; | |
39 unsigned c; | |
40 int n = 1; | |
41 double t1, t2; | |
42 t1 = gettimeofday_sec(); | |
43 for(c=0;c<INT_MAX;c++){ | |
44 n = calc(o, selector, n); | |
45 } | |
46 t2 = gettimeofday_sec(); | |
47 printf("%d\nObjective-C\t%f\n", n, t2 - t1); | |
48 return 0; | |
49 } |