> I started with Ryan's code from `~r` and tried to emulate some special
> cases I see the C code (and didn't worry about negative numbers) and
> ended up with something 4-5x slower than the C code version. :(  Code
> follows.


Is it cheating to avoid divisions by using table lookups? 

cpu time: 1541 real time: 1557 gc time: 17 ; number->string
cpu time: 6449 real time: 6447 gc time: 48 ; number->string* (Findler algorithm)
cpu time: 1788 real time: 1848 gc time: 55 ; number->string** (with table 
lookups)

;;;;;;;;;;;;;;;;;;;
#lang racket

(define (number->string* N)
  (cond [(zero? N)
         (string #\0)]
        [else
         (let* ([short-size 10]
                [str (make-string short-size)])
           (let loop ([N N] [digits null] [i short-size])
             (cond
               [(zero? N)
                (if (<= i 0)
                    (if (null? digits)
                        str
                        (string-append (apply string digits) str))
                    (substring str i short-size))]
               [else
                (define-values (q r) (quotient/remainder N 10))
                (define d (integer->char (+ r (char->integer #\0))))
                (cond
                  [(<= i 0)
                   (loop q (cons d digits) i)]
                  [else
                   (string-set! str (- i 1) d)
                   (loop q '() (- i 1))])])))]))


(define-syntax-rule (digit-vector id ...)
  (for*/vector ([id (in-range 10)] ...)
               (append*
                (for/list ([x (in-list (list id ...))])
                          (string->list (number->string x))))))

(define number->string**
  (let ([digits (digit-vector i)]
        [double-digits (digit-vector i j)]
        [triple-digits (digit-vector i j k)])
    (λ (N)
      (list->string
       (let loop ([N N])
         (cond
           [(< N 10) (vector-ref digits N)]
           [(< N 100) (vector-ref double-digits N)]
           [(< N 1000) (vector-ref triple-digits N)]
           [else
            (append (loop (quotient N 1000))
                    (vector-ref triple-digits (remainder N 1000)))]))))))

(define-syntax-rule (test-it id ...)
  (begin
    (module+ test
      (require rackunit)
      (check-equal? (id 1234567890987654321)
                    (number->string 1234567890987654321))
      (for ([x (in-range 10000)])
           (check-equal? (id x )
                         (number->string x)))) ...))

(test-it number->string* number->string**)

(define iterations 10000000)
(define number-to-convert 1234567890)

(define-syntax-rule (time-it id ...)
  (begin
    (begin
      (collect-garbage)
      (time
       (for ([x (in-range iterations)])
            (id x)))) ...))

(time-it number->string number->string* number->string**)

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to