[gambit-list] code optimization & compilation

Marc Feeley feeley at iro.umontreal.ca
Tue Apr 9 09:26:07 EDT 2013


On 2013-04-09, at 12:36 AM, Guillaume Lathoud <glathoud at yahoo.fr> wrote:

> Hello,
> 
> on a simple task - multiply two matrices mat_a and mat_b https://github.com/glathoud/flatorize/blob/master/explore/scheme_matmul_common.scm - I've been comparing 3 implementations, using interpretation and compilation.
> 
> To compile I used the line below. Is there a better way, at least a few straightforward optimization (options) ?
> gsc -exe -o scheme_matmul_classic.bin tmp.scm
> Details are below.
> 
> Best regards,
> Guillaume

There are many things you can do to improve the performance of your code, but I wonder what you are trying to achieve.  For instance, your code is multiplying matrices of small exact integers.  Is this a given in your problem or you want the code to work also for floating point numbers?  Also, do you want the code to conform to a specific standard of Scheme, or you are willing to adapt your code to a specific Scheme implementation?  For Gambit you could store the numbers in a homogeneous numerical vector (e.g. s16vector, f64vector, etc) instead of a plain vector, and use fixnum specific operations for computing the indices of the matrix.  Finally, you should avoid using assignments if you can use iteration variables instead.  So, instead of

  (let ((sum 0))
    (let loop ((i 0))
      (if (< i n)
          (begin
            (set! sum (+ sum i))
            (loop (+ i 1)))))
    sum)

you should write

  (let loop ((i 0) (sum 0))
    (if (< i n)
        (loop (+ i 1) (+ sum i))
        sum))

Marc




More information about the Gambit-list mailing list