diff Looping.M @ 0:2216535ade9c

メンバー関数/メソッド呼び出しのベンチマーク。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Tue, 15 Feb 2011 13:47:19 +0900
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.M	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,45 @@
+#import <stdio.h>
+#import <objc/Object.h>
+#import <limits.h>
+#import <sys/time.h>
+
+@interface Looping : Object {
+  int n0;
+}
+- (id)init;
+- (int)calc:(int)n;
+@end
+
+@implementation Looping
+- (id)init{
+  [super init];
+  n0 = 0;
+  return self;
+}
+- (int)calc:(int)n {
+  int n1 = n0 + (1 - 2*(n%2));
+  n0 = n;
+  return n1;
+}
+@end
+
+double gettimeofday_sec()
+{
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return tv.tv_sec + (double)tv.tv_usec*1e-6;
+}
+
+int main(){
+  id o = [[Looping alloc] init];
+  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\nObjective-C\t%f\n", n, t2 - t1);
+  return 0;
+}