[racket] Performance. Higher-order function

From: Roman Klochkov (kalimehtar at mail.ru)
Date: Sun Aug 3 05:15:19 EDT 2014

Are higher order function always slow?

Made small test:
test1 -- unfamous set-in-the-loop accumulating
test2 -- built-in build-string
test3 -- manually written build-string with the same code as in test1
----
(define (test1 n)
(define res (make-string n))
    (for ([i (in-range n)])
        (string-set! res i (integer->char i)))
res)
(define (test2 n)
    (build-string n integer->char))
(define (build-string1 n proc)
    (define res (make-string n))
    (for ([i (in-range n)])
        (string-set! res i (proc i)))
    res)
(define (test3 n)
    (build-string1 n integer->char))
(time (for ([i 100000]) (test1 100)))
(time (for ([i 100000]) (test1 100)))
(time (for ([i 100000]) (test1 100)))
(displayln "")
(time (for ([i 100000]) (test2 100)))
(time (for ([i 100000]) (test2 100)))
(time (for ([i 100000]) (test2 100)))
(displayln "")
(time (for ([i 100000]) (test3 100)))
(time (for ([i 100000]) (test3 100)))
(time (for ([i 100000]) (test3 100)))
----
Tested on Linux x32
$ /usr/racket/bin/racket 
Welcome to Racket v6.1.
> (enter! "test") 
cpu time: 360 real time: 469 gc time: 64
cpu time: 212 real time: 209 gc time: 0
cpu time: 208 real time: 208 gc time: 0

cpu time: 400 real time: 402 gc time: 0
cpu time: 380 real time: 382 gc time: 4
cpu time: 384 real time: 383 gc time: 0

cpu time: 524 real time: 529 gc time: 4
cpu time: 468 real time: 470 gc time: 8
cpu time: 412 real time: 414 gc time: 12

---

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.
-- 
Roman Klochkov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140803/e347ffb9/attachment.html>

Posted on the users mailing list.