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.