#import <Foundation/Foundation.h>
#include <stdio.h>
@interface Rectangle : NSObject {
@public
int x;
int y;
int w;
int h;
}
-(int)intersect:(Rectangle*)r1;
@end
@implementation Rectangle
-(int)intersect:(Rectangle*)r
{
int tw = w;
int th = h;
int rw = r->w;
int rh = r->h;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return 0;
}
int tx = x;
int ty = y;
int rx = r->x;
int ry = r->y;
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry));
}
@end
int main (int argc, const char * argv[]) {
Rectangle* r1 = [Rectangle new];
Rectangle* r2 = [Rectangle new];
NSDate *start = [NSDate date];
for (int i = 0; i < 500*1000000; i++) {
r1->x = i;
r1->y = i;
r1->w = i / 4;
r1->h = i / 4;
r2->x = i + i / 8;
r2->y = i + i / 8;
r2->w = i / 2;
r2->h = i / 2;
int b = [r1 intersect: r2];
}
NSDate *stop = [NSDate date];
NSTimeInterval duration = [stop timeIntervalSinceDate:start];
printf("time: %g\n", duration);
return 0;
}
Java:
public class Test {
static class Rectangle {
int x;
int y;
int w;
int h;
public boolean intersects(Rectangle r) {
int tw = this.w;
int th = this.h;
int rw = r.w;
int rh = r.h;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return false;
}
int tx = this.x;
int ty = this.y;
int rx = r.x;
int ry = r.y;
rw += rx;
rh += ry;
tw += tx;
th += ty;
// overflow || intersect
return ((rw < rx || rw > tx) &&
(rh < ry || rh > ty) &&
(tw < tx || tw > rx) &&
(th < ty || th > ry));
}
}
public static void main(String[] a) {
Rectangle r1 = new Rectangle();
Rectangle r2 = new Rectangle();
long start = System.nanoTime();
for (int i = 0; i < 500*1000000; i++) {
r1.x = i;
r1.y = i;
r1.w = i / 4;
r1.h = i / 4;
r2.x = i + i / 8;
r2.y = i + i / 8;
r2.w = i / 2;
r2.h = i / 2;
boolean b = r1.intersects(r2);
}
long stop = System.nanoTime();
double duration = (stop - start) / 1000000000.;
System.out.println("time: " + duration);
}
}
both run on iMac
Model Name: iMac
Model Identifier: iMac10,1
Processor Name: Intel Core 2 Duo
Processor Speed: 3.06 GHz
Number Of Processors: 1
Total Number Of Cores: 2
L2 Cache: 3 MB
Memory: 4 GB
Bus Speed: 1.07 GHz
Yield:
ObjectiveC: 7.73261 seconds
Java: 2.303848 seconds
wall time.
5 comments:
I would like to see the timings from cold start to application exit.
We are talking about language performance here or about how slow Apple can make the JVM to load from disk? Apple can win at that game (they can make it even slower if Steve Jobs will ask nicely). But language wise (as CPU instructions per second) Obj C is not a champion. I will try the same test in C next.
interesting. any idea where is all the time lost by obj c?
also, how's that c test looking?
surely can't be any VM kind of overhead there?
I am not an expert in ObjC (did not read the disassembly yet) but my first guess is that V-table in ObjC is a modifiable (via "isa" pointer) hash map which means that any access to a method or property is actually indexing hash map (should be a single index op in array followed by strcmp). However smart enough runtime/compiler can optimize it out for private non-virtual method (they way Java does) and replace it with direct memory access. What I really don't like is Steve Jobs claims that Obj C is fast and Java is slow. I don't like people (especially bosses) being ignorant and arrogant at the same time. But than, somebody else, probably writes his speeches for him.
Post a Comment