Sunday, December 26, 2010

plain C versus Java performance.

this is follow up on Objective C vs Java performance

In plain C:


#include <stdio.h>
#include <stdlib.h>
#include <CoreServices/CoreServices.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
#include <unistd.h>

typedef struct {
int x;
int y;
int w;
int h;
} Rectangle;

int intersect(Rectangle* t, Rectangle* r)
{
int tw = t->w;
int th = t->h;
int rw = r->w;
int rh = r->h;
if (rw <= 0 || rh <= 0 || tw <= 0 || th <= 0) {
return 0;
}
int tx = t->x;
int ty = t->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));
}


int main (int argc, const char * argv[]) {
Rectangle* r1 = malloc(sizeof(Rectangle));
Rectangle* r2 = malloc(sizeof(Rectangle));
int i;
uint64_t start, stop, elapsed;
Nanoseconds nano;
double duration;
start = mach_absolute_time();
for (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 = intersect(r1, r2);
}
stop = mach_absolute_time();
elapsed = stop - start;
nano = AbsoluteToNanoseconds( *(AbsoluteTime *) &elapsed );
duration = (*(uint64_t*)&nano) / 1000000000.;
printf("time: %g\n", duration);
return 0;
}


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
Plain C: 1.39241 seconds
wall time.

Objective C versus Java performance.

Objective C:

#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.