(use-modules (charting) (system base pmatch)) (define-syntax time (syntax-rules () ((_ expr ...) (let ((t0 (get-internal-real-time))) expr ... (let ((t1 (get-internal-real-time))) (/ (- t1 t0) 1.0 internal-time-units-per-second)))))) (with-output-to-file "/tmp/g.c" (lambda () (display "unsigned int g (void) { return 1; }"))) (system "gcc -c -o /tmp/g.o /tmp/g.c") (define (test-gcc-comp n) (define prog (format #f "extern unsigned int g (void); int main (int argc, char *argv) { unsigned int i, ret = 0; for (i = 0; i < ~AU; i++) ret += g (); return ret; }" n)) (with-output-to-file "/tmp/test.c" (lambda () (display prog))) (time (system "gcc -O2 -g -o /tmp/test /tmp/test.c /tmp/g.o"))) (define (test-gcc-run) (time (system "/tmp/test"))) (define (test-v8 n) (define prog (format #f "function g () { return 1; } function f () { var ret = 0; for (var i = 0; i < ~A; i++) { ret += g (); } return ret; } f (); " n)) (with-output-to-file "/tmp/test.js" (lambda () (display prog))) (time (system "~/src/v8/shell /dev/stdin < /tmp/test.js"))) (define (run-test) (map (lambda (pow) (let* ((n (ash 1 pow)) (comp (test-gcc-comp n))) (list pow n (test-v8 n) comp (test-gcc-run)))) (iota 32))) (define (make-chart data) ((@ (charting draw) reset-colors!)) (make-bar-chart "V8 vs GCC" (map (lambda (x) (pmatch x ((,pow ,n ,v8 ,gcc-comp ,gcc-run) (let ((gcc (+ gcc-comp gcc-run 0.0))) (list (number->string pow) (list (/ v8 gcc) "V8 compile and run time") (list (/ gcc-comp gcc) "GCC compile time only (without running)")))))) data) "v8-vs-gcc.png" #:bar-width 5 #:group-spacing 10 #:chart-params '(#:x-axis-label "number of iterations (2**N)" #:y-axis-label "% of gcc compilation + run"))) (define data (run-test)) (with-output-to-file "test.out" (lambda () (display "((pow n v8 gcc-comp gcc-run) ...)\n") (display data))) (make-chart data)