El 2 de abril de 2010 19:17, Bradley Lucier lucier@math.purdue.eduescribió:
On Apr 2, 2010, at 11:59 AM, Álvaro Castro-Castilla wrote:
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
(floor (/ i size-y))))))))
(/ i size-y) is a rational number
(make-point 200 200) keeps making the same point over and over again; in C you make the point once.
Try
(declare (standard-bindings)(extended-bindings)(block)(not safe))
(define-structure point x y)
(define (fxsquare x) (fx* x x))
(define (distance-point-point-integer a b) (##flonum.->fixnum (flsqrt (fixnum->flonum (fx+ (fxsquare (fx- (point-x a) (point-x b))) (fxsquare (fx- (point-y a) (point-y b))))))))
(define (make-2d-field-v2 size-x size-y proc) (let ((point (make-point 0 0))) (let ((len (fx* size-x size-y)))
(do ((vec (make-u8vector len)) (i 0 (fx+ i 1))) ((fx>= i len) vec) (point-x-set! point (fxmodulo i size-y)) (point-y-set! point (fxquotient i size-y)) (u8vector-set! vec i (proc point))))))
(time (make-2d-field-v2 500 500 (let ((stationary-point (make-point 200 200))) (lambda (p) (let ((d (distance-point-point-integer p stationary-point))) (if (fx> d 255) 255 d)))) ) )
Thank you very much.
The clean benchmark seams to be only 30% slower in scheme compared to C. That's good enough for me. I've been also reorganizing my application code to compile those critical parts and seems to make a nice improvement. And learnt also things about optimizing scheme/gambit code.
Best regards,
Álvaro Castro-Castilla