<div dir="ltr">Hi gambit users<div><br></div><div>I am trying to verify that a program written in Gambit-C scheme is as fast as equivalent C program.</div><div><br></div><div>I tried to make a scheme version of the following C++ program which is;</div><div><br></div><div><div><b>#include <iostream></b></div><div><b>#include <cmath></b></div><div><b>#include <cstdio></b></div><div><b>#include <ctime></b></div><div><b>#include <cstdlib></b></div><div><b>#include <iomanip>  </b></div><div><b><br></b></div><div><b>using namespace std;</b></div><div><b><br></b></div><div><b>double f(double x);</b></div><div><b><br></b></div><div><b>int main(int argc, char* argv[]) {</b></div><div><b>  clock_t start;</b></div><div><b>  start = clock();</b></div><div><b>  double duration;</b></div><div><b>  double sum = 0;</b></div><div><b>  long n = atoi(argv[1]);</b></div><div><b>  double x;</b></div><div><b>  for(long i = 1; i <= n ;i++) {</b></div><div><b>    x = (i - 0.5) / n;</b></div><div><b>    sum += 4.0/(1.0+x*x);</b></div><div><b>  }</b></div><div><b>  sum /= n;</b></div><div><b>  cout << setprecision(17) << sum << endl << endl;</b></div><div><b><br></b></div><div><b>  duration = ( clock() - start ) / (double) CLOCKS_PER_SEC;</b></div><div><b>  cout << duration <<'\n';</b></div><div><b>  return 0;</b></div><div><b>}</b></div><div><b><br></b></div><div><b>double f(double x) {</b></div><div><b>  return 4.0/(1.0 + x*x);</b></div><div><b>}</b></div></div><div><br></div><div>and my best so far is</div><div><div><b>#!/usr/bin/env gsi-script</b></div><div><b>(declare</b></div><div><b>  (not safe)</b></div><div><b>  (mostly-flonum))</b></div><div><b>(define (main arg)</b></div><div><b>  (let ((k (string->number arg)))</b></div><div><b>    (pretty-print (time (cpi (exact->inexact k))))))</b></div><div><b>(define (cpi n)</b></div><div><b>  (letrec ((rec (lambda (i sum)</b></div><div><b>                  (let* ((x (fl/ (fl- i 0.5) n))</b></div><div><b>                         (summand (fl/ 4.0 (fl+ 1.0 (fl* x x)))))</b></div><div><b>                    (if (fl> i n)</b></div><div><b>                        (fl/ sum n)</b></div><div><b>                        (rec (fl+ i 1.0) (fl+ sum summand)))))))</b></div><div><b>    (rec 0.0 0.0)))</b></div></div><div><br></div><div>and the result is poor: 0.043s vs 0.145s</div><div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ ls</b></div><div><b>pi.cpp  pi.scm</b></div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ g++ -o pi-cpp pi.cpp</b></div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ gsc -exe -o pi-scm pi.scm</b></div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ time ./pi-cpp 1000000</b></div><div><b>3.1415926535897643</b></div><div><b><br></b></div><div><b>0.038903</b></div><div><b><br></b></div><div><b>real<span class="" style="white-space:pre">      </span><u>0m0.043s</u></b></div><div><b>user<span class="" style="white-space:pre"> </span>0m0.042s</b></div><div><b>sys<span class="" style="white-space:pre">     </span>0m0.004s</b></div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ time ./pi-scm 1000000</b></div><div><b>(time (cpi (exact->inexact k)))</b></div><div><b>    128 ms real time</b></div><div><b>    128 ms cpu time (127 user, 2 system)</b></div><div><b>    182 collections accounting for 67 ms real time (72 user, 1 system)</b></div><div><b>    224000448 bytes allocated</b></div><div><b>    341 minor faults</b></div><div><b>    no major faults</b></div><div><b>3.1415966535897644</b></div><div><b><br></b></div><div><b>real<span class="" style="white-space:pre">       </span><u>0m0.145s</u></b></div><div><b>user<span class="" style="white-space:pre"> </span>0m0.136s</b></div><div><b>sys<span class="" style="white-space:pre">     </span>0m0.009s</b></div><div><b>server@HP-Proliant-MicroServer:~/speedtest$ </b></div></div><div><br></div><div>So.. I guess my scheme version is not equivalent to the original C++ program,</div><div>but I'm having trouble figuring out why</div><div><br></div><div>any help will be appreciated</div><div><br></div><div>any comment, any suggestion..</div></div>