[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] About peformance of user defined procedures

From: Jörg F . Wittenberger
Subject: Re: [Chicken-users] About peformance of user defined procedures
Date: 01 Aug 2011 22:05:13 +0200

On Aug 1 2011, Pedro Henrique Antunes de Oliveira wrote:

Interesting point.

But I tried it out. In average, it took about the same amount of time
(actualy this was about 0.1s slower).

Looking closer (since you made me curious); there is little magic
about the definition of "map" as far as I can see.
No special code generation, just:

(define (##sys#map p lst0)
 (let loop ((lst lst0))
   (cond ((eq? lst '()) lst)
          ((pair? lst)
           (cons (p (##sys#slot lst 0)) (loop (##sys#slot lst 1))) )
          (else (##sys#error-not-a-proper-list lst0 'map)) ) ))

(define map)

(##sys#slot lst 0) is in effect (car lst) sans any kind of checks
or calls.  Maybe this helps?

Maybe because the amount of time spent on binding arguments to
parameters is larger than the amount of time of that (procedure? op).

I've heard that some compilers can do something like memoization for
stuff like (procedure? op) (it is possible for that to be done in this
case), which would speed that up.

I've tried compiling with csc -O3 -unsafe (to avoid those checks if
something is a procedure or not), and no actual difference too.

On Mon, Aug 1, 2011 at 2:36 PM, Jörg F. Wittenberger
<address@hidden> wrote:
just a wild guess:

On Jul 31 2011, Pedro Henrique Antunes de Oliveira wrote:


I have a file map.scm, which contais this code:

(define (mymap1 op ls)
 (let loop ((ls ls) (acc '()))
  (if (null? ls)
      (reverse acc)
      (loop (cdr ls) (cons (op (car ls)) acc)))))

This compiles (*probably* and depending on optimisation switches you pass
to the compiler) one call equivalend to (procedure? op).

Since everything around is zero-effort, you basically check loop and
call performance here.

You might also find it funny to try:

(define (mymap1b op ls)
 (let loop ((op op) (ls ls) (acc '()))
  (if (null? ls)
      (reverse acc)
      (loop op (cdr ls) (cons (op (car ls)) acc)))))

which would avoid one indirection per call at the expense of
yet another loop parameter.  Please let me know how much of a difference
that would be.

(define (mymap2 op ls)
 (let loop ((ls ls))
  (if (null? ls)
      (cons (op (car ls)) (loop (cdr ls))))))

(define (mymap3 op ls)
 (if (null? ls)
    (cons (op (car ls)) (mymap3 op (cdr ls)))))

(define ls (let loop ((i 0)) (if (= i 1000000) '() (cons i (loop (add1

And another four files, f1.scm, f2.scm, f3.scm, f4.scm.

(include "map.scm")
(map add1 ls)

(include "map.scm")
(mymap1 add1 ls)

(include "map.scm")
(mymap2 add1 ls)

(include "map.scm")
(mymap3 add1 ls)

Compiling all four f[1-4].scm files, with csc -O3, I got those results:

f1 took 0.95secs (average)
f2 took 1.65secs (average)
f3 took 1.35secs (average)
f4 took 1.35secs (average)

I understand why f4 and f3 are pretty much the same thing, but what
differs from mine to the built in map that makes the built in so
faster (2-3x faster)?

Interpreted languages have this characteristic that built in
procedures tend to be much faster, but this all is compiled. I suppose
it is possible to make procedures, in chicken/scheme that are as fast
as the built in one. Right?

Note: compiling with -O5 instead of -O3 made the programs 0.1secs "shorter". Note2: this is not about map specifically (I've been looking at some procedures that I have that look somewhat to some built in ones, but are much slower)

Chicken-users mailing list

reply via email to

[Prev in Thread] Current Thread [Next in Thread]