Hello,

First let's see how 'partition' from SRFI-1 does on allocation:

(import (except (rnrs) partition)
        (only (srfi :1) iota partition)
        (only (ikarus) time))

(let ((data (iota 1000)))
  (time (partition odd? data)))

~ # ikarus --r6rs-script /scratch/_partition-srfi-1-a.sps
running stats for (partition odd? data):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    40008 bytes allocated

The (rnrs) library also provides a 'partition':

(import (rnrs)
        (only (srfi :1) iota)
        (only (ikarus) time))

(let ((data (iota 1000)))
  (time (partition odd? data)))

~ # ikarus --r6rs-script /scratch/_partition-rnrs-a.sps
running stats for (partition odd? data):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    28016 bytes allocated

Much better.

I was messing around with another version of 'partition'. This one takes a third parameter instead of returning two values. You can emulate the RNRS/SRFI version simply by passing 'values' as the third argument.

Let's see how it does:

(import (except (rnrs) partition)
        (only (srfi :1) iota)
        (only (ikarus) time))

(define (partition fun lis c)
  (cond ( (null? lis) (c '() '()) )
        ( (fun (car lis))
          (partition fun
                     (cdr lis)
                     (lambda (a b)
                       (c (cons (car lis) a)
                          b))) )
        ( else
          (partition fun
                     (cdr lis)
                     (lambda (a b)
                       (c a
                          (cons (car lis) b)))) )))

(let ((data (iota 1000)))
  (time (partition odd? data values)))

~ # ikarus --r6rs-script /scratch/_partition-a.sps
running stats for (partition odd? data values):
    no collections
    0 ms elapsed cpu time, including 0 ms collecting
    0 ms elapsed real time, including 0 ms collecting
    24016 bytes allocated

I.e. it does the least allocation. This was a surprise.

Ed

Reply via email to