<HTML><BODY><p>Are higher order function always slow?<br><br>Made small test:<br>test1 -- unfamous set-in-the-loop accumulating<br>test2 -- built-in build-string<br>test3 -- manually written build-string with the same code as in test1<br>----<br>(define (test1 n)<br> (define res (make-string n))<br>    (for ([i (in-range n)])<br>        (string-set! res i (integer->char i)))<br> res)</p><p>(define (test2 n)<br>    (build-string n integer->char))</p><p>(define (build-string1 n proc)<br>    (define res (make-string n))<br>    (for ([i (in-range n)])<br>        (string-set! res i (proc i)))<br>    res)</p><p>(define (test3 n)<br>    (build-string1 n integer->char))</p><p>(time (for ([i 100000]) (test1 100)))<br>(time (for ([i 100000]) (test1 100)))<br>(time (for ([i 100000]) (test1 100)))<br>(displayln "")<br>(time (for ([i 100000]) (test2 100)))<br>(time (for ([i 100000]) (test2 100)))<br>(time (for ([i 100000]) (test2 100)))<br>(displayln "")<br>(time (for ([i 100000]) (test3 100)))<br>(time (for ([i 100000]) (test3 100)))<br>(time (for ([i 100000]) (test3 100)))<br>----<br>Tested on Linux x32</p><div></div><div><div>$ /usr/racket/bin/racket </div><div>Welcome to Racket v6.1.</div><div>> (enter! "test") </div><div>cpu time: 360 real time: 469 gc time: 64</div><div>cpu time: 212 real time: 209 gc time: 0</div><div>cpu time: 208 real time: 208 gc time: 0</div><div><br></div><div>cpu time: 400 real time: 402 gc time: 0</div><div>cpu time: 380 real time: 382 gc time: 4</div><div>cpu time: 384 real time: 383 gc time: 0</div><div><br></div><div>cpu time: 524 real time: 529 gc time: 4</div><div>cpu time: 468 real time: 470 gc time: 8</div><div>cpu time: 412 real time: 414 gc time: 12<br><br>---<br><br></div></div><div>So I see, that build-string version is about two times slower, than set-in-the-loop. Why so much? I expected about 10-20% difference.</div><br>-- <br>Roman Klochkov</BODY></HTML>