changeset 0:2216535ade9c

メンバー関数/メソッド呼び出しのベンチマーク。
author "uncorrelated zombie" <uncorrelated@yahoo.co.jp>
date Tue, 15 Feb 2011 13:47:19 +0900
parents
children 9d6fb29e41ce
files .hgignore Looping.M Looping.c Looping.cpp Looping.java Looping.py Makefile
diffstat 7 files changed, 196 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgignore	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,2 @@
+\.class$
+\.exe$
--- /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;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.c	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,41 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <limits.h>
+#include <sys/time.h>
+
+typedef struct {
+	int n0;
+} Looping;
+
+int calc(Looping *s, int n){
+	int n1 = s->n0 + (1 - 2*(n%2));
+	s->n0 = n;
+	return n1;
+}
+
+double gettimeofday_sec()
+{
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return tv.tv_sec + (double)tv.tv_usec*1e-6;
+}
+
+int main(){
+  Looping *o;
+  unsigned c;
+  int n = 1;
+  double t1, t2;
+
+  o = (Looping *)malloc(sizeof(Looping));
+  o->n0 = 0;
+
+  t1 = gettimeofday_sec();
+  for(c=0;c<INT_MAX;c++){
+    n = calc(o, n);
+  }
+  t2 = gettimeofday_sec();
+  printf("%d\nC\t%f\n", n, t2 - t1);
+
+  free(o);
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.cpp	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,43 @@
+#include <stdio.h>
+#include <limits.h>
+#include <sys/time.h>
+
+class Looping {
+public:
+	Looping();
+	int calc(int n);
+private :
+	int n0;
+};
+
+Looping::Looping(){
+	n0 = 0;
+}
+
+int Looping::calc(int n){
+	int n1 = n0 + (1 - 2*(n%2));
+	n0 = n;
+	return n1;
+}
+
+double gettimeofday_sec()
+{
+  struct timeval tv;
+  gettimeofday(&tv, NULL);
+  return tv.tv_sec + (double)tv.tv_usec*1e-6;
+}
+
+int main(){
+  Looping *o = new Looping();
+  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\nC++\t%f\n", n, t2 - t1);
+  delete o;
+  return 0;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.java	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,23 @@
+
+public class Looping {
+	private int n0;
+	public Looping(){
+		n0 = 0;
+	}
+	public int calc(int n){
+		int n1 = n0 + (1 - 2*(n%2));
+		n0 = n;
+		return n1;
+	}
+	public static void main(String[] args) {
+		Looping l = new Looping();
+		int n = 1;
+		long t1, t2;
+		t1 = System.currentTimeMillis();
+		for(int c=0;c<Integer.MAX_VALUE;c++){
+			n = l.calc(n);
+		}
+		t2 = System.currentTimeMillis();
+		System.out.println(n + "\nJava\t" + (float)(t2 - t1)/1000);
+	}
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Looping.py	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,22 @@
+#!/usr/bin/env python
+import time
+
+class Looping:
+	"Looping Class"
+	n0 = 0
+	def calc(self, n):
+		n1 = self.n0 + (2 - 2*(n%2));
+		self.n0 = n;
+		return n1
+
+if __name__ == "__main__":
+	s = Looping()
+	c = 0
+	n = 1
+	t1 = time.time()
+	while c<2147483647:
+		n = s.calc(n)
+		c = c + 1
+	t2 = time.time()
+	print n, "Python\t", (t2 - t1)
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Makefile	Tue Feb 15 13:47:19 2011 +0900
@@ -0,0 +1,20 @@
+FLAGS= -O2 -s
+ALL: Looping_c.exe Looping_c++.exe Looping_objc.exe Looping_objc-IMP.exe Looping.class
+Looping_c.exe : Looping.c
+	gcc -o Looping_c.exe Looping.c $(FLAGS)
+
+Looping_c++.exe : Looping.cpp
+	g++ -o Looping_c++.exe Looping.cpp $(FLAGS)
+
+Looping_objc.exe : Looping.M
+	g++ -o Looping_objc.exe Looping.M -lobjc $(FLAGS)
+
+Looping_objc-IMP.exe : Looping-IMP.M
+	g++ -o Looping_objc-IMP.exe Looping-IMP.M -lobjc $(FLAGS)
+
+Looping.class : Looping.java
+	javac Looping.java
+
+clean:
+	rm -f *.exe *.class
+