El 2 de abril de 2010 17:11, Bradley Lucier <lucier@math.purdue.edu> escribió:
This function in C

int distance_point_point(struct point *p1, struct point *p2) {
       return sqrt( pow( p1->x - p2->x, 2) + pow( p1->y - p2->y, 2) );
}

is translated quite literally into (Gambit) scheme as

(define (distance-point-point a b)
 (##flonum.->fixnum (flsqrt (fl+ (flexpt (fixnum->flonum (point-x a)) 2.)
                                 (flexpt (fixnum->flonum (point-y a)) 2.)))))

The C type inference rules, the automatic conversion of int to double and back, and the header <math.h> takes care of all of that.

And you're allocating manipulating lists in the Scheme version, but using an array in the C version, which you could also do in the scheme version.

So these codes are in only a rough sense "equivalent".

If you add the declarations

 (declare (standard-bindings)(extended-bindings)(block)(not safe))

then things should go a bit faster.

Brad

Actually, I tried that code, and this runs faster:

(define (distance-point-point-integer a b)
  (integer-sqrt (fx+ (expt (- (point-x a) (point-x b)) 2)
                           (expt (- (point-y a) (point-y b)) 2))))

What does actually help (~2x) is the declaration: (declare (standard-bindings)(extended-bindings)(block)(not safe))

Now I have it down to ~350ms. which is 10-20x the C version. with floating point. I can't understand how the straightforward u8vector version (the second in the scheme code) is slower (~500ms) than the same thing with consed lists and afterwards transformed to a u8vector :S


Best regards