[gambit-list] Performance

Marc Feeley feeley at iro.umontreal.ca
Fri Jul 28 22:02:04 EDT 2023


> On Jul 28, 2023, at 4:36 PM, Torbjörn Svensson Diaz <torbjorn_svensson_diaz at yahoo.com> wrote:
> 
> Hello!
> 
> "With appropriate declarations in the source code the compiled Scheme programs run roughly as fast as equivalent C programs." This is from the manual.
> 
> What is "roughly as fast"? 90% of C speed? 50% of C speed? Are there some benchmarks?
> 
> Best regards,
> 
> 
> -- 
> 
> T.

I don’t have a comprehensive set of benchmarks, as it would require writing the same program in both C and Scheme to do a thorough comparison.

However, here is one of my favourite examples of a short Scheme program that actually runs 15% faster than the equivalent C program (this was obtained on a macOS computer with M2 processor and Gambit v4.9.5).

Marc


In Scheme compiled with Gambit which was built with "gcc-13 -O1":

$ cat scm-fib.scm
(declare (standard-bindings) (block) (fixnum) (not safe))

(define (fib n)
  (if (< n 2)
      n
      (+ (fib (- n 1)) (fib (- n 2)))))

(define (main . args)
  (let ((n (if (pair? args) (string->number (car args)) 40)))
    (println "fib(" n ") = " (fib n))))

(apply main (cdr (command-line)))
$ gsc -exe scm-fib.scm;time ./scm-fib 42
fib(42) = 267914296

real    0m1.021s
user    0m0.933s
sys     0m0.007s


In C compiled with "gcc-13 -O1" and "clang -O3":

$ cat c-fib.c
#include <stdio.h>
#include <stdlib.h>

int fib(int n) {
  if (n < 2)
    return n;
  else
    return fib(n-1) + fib(n-2);
}

int main(int argc, char *argv[]) {
  int n = 40;
  if (argc == 2) n = atoi(argv[1]);
  printf("fib(%d) = %d\n", n, fib(n));
  return 0;
}
$ gcc-13 -o c-fib c-fib.c;time ./c-fib 42
fib(42) = 267914296

real    0m1.181s
user    0m1.101s
sys     0m0.003s
$ clang -O3 -o c-fib c-fib.c;time ./c-fib 42
fib(42) = 267914296

real    0m1.138s
user    0m0.843s
sys     0m0.004s

P.S. "gcc-13 -O2" does generate a faster executable than Gambit… my point is that the performance can be comparable to the speed of C (and in this example it beats clang regardless of its optimization level).


More information about the Gambit-list mailing list