I'm not too worried about a x4 slowdown in number->string because I
don't expect it to be in a tight loop, but it would be nice that it
were faster.

I made some tests. (You must change the 100 to number-to-convert in
your sample program.)

I made a new function number->string**, replacing
    (define-values (q r) (quotient/remainder N 10))
with
    (define q (quotientr N 10))
    (define r (remainder N 10))

The problem is that quotient/remainder is very slow for fixnums (IIRC
it can be JIT-inlined).


I increased the number of iterations x10, so the run time is a few seconds:
(define iterations 10000000) ; x10 of original number
(define number-to-convert 123)
cpu time: 1734 real time: 1740 gc time: 0   ;number->string
cpu time: 7234 real time: 7258 gc time: 0   ;number->string*
cpu time: 3578 real time: 3580 gc time: 31  ;number->string**

So the new function is only x2 times slower. Perhaps tweaking it for
fixnums can get a 10% or 20% increase of speed.


But for bignum the story is totally different. Here I used the
original number of iterations because everything is much slower.
(define iterations 1000000) ; original number
(define number-to-convert 1234567890123456789012345678901234567890)
cpu time: 1125 real time: 1128 gc time: 16   ;number->string
cpu time: 14828 real time: 14818 gc time: 94   ;number->string*
cpu time: 20735 real time: 20736 gc time: 222  ;number->string**

The new function is even slower. But both functions are very slow
(x15) in comparison with the C version.

(Note: To compare the times remember to consider the x10 factor in the
iterations, for example the run time of the original C function is
proportional to 1734 vs 11250, as expected it's faster for the
fixnum.)

Gustavo



On Wed, Dec 28, 2016 at 5:41 PM, Robby Findler
<ro...@eecs.northwestern.edu> wrote:
> 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.
>
> Robby
>
> #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))])])))]))
>
> (module+ test
>   (require rackunit)
>   (check-equal? (number->string* 1234567890987654321)
>                 (number->string 1234567890987654321))
>   (for ([x (in-range 1000)])
>     (check-equal? (number->string* x )
>                   (number->string x))))
>
> (define iterations 1000000)
> (define number-to-convert 123)
>
> (collect-garbage)
> (time
>  (for ([x (in-range iterations)])
>    (number->string 100)))
>
> (collect-garbage)
> (time
>  (for ([x (in-range iterations)])
>    (number->string* number-to-convert)))
>
> --
> 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.

-- 
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