> On Dec 31, 2016, at 12:05 PM, Robby Findler <ro...@eecs.northwestern.edu> > wrote: > > But this introduces an additional cost that's a little bit more > troublesome to measure. Specifically, each namespace that instantiates > `racket/base` will now take a tiny bit more space and take a tiny bit > longer to initialize.
> As one data point, here's a histogram of the 20k or so calls to > number->string that happen during the start up of DrRacket (first > entry in each list is the argument passed to number->string and the > second is the number of times that call happened) Hmm, well if the distribution of inputs is that imbalanced, perhaps this is the better cheat: ; 100,000 conversions of 100 random integers < 123 cpu time: 779 real time: 791 gc time: 16 ; number->string cpu time: 769 real time: 771 gc time: 13 ; number->string** ; 100,000 conversions of 100 random integers < 123456 cpu time: 1365 real time: 1384 gc time: 22 ; number->string cpu time: 941 real time: 955 gc time: 13 ; number->string** ; 100,000 conversions of 100 random integers < 123456789 cpu time: 1891 real time: 1904 gc time: 26 ; number->string cpu time: 963 real time: 979 gc time: 17 ; number->string** ;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #lang racket (define number->string** (let ([digits (list->vector (string->list "0123456789"))] [cache (make-hasheqv)]) (λ (N) (hash-ref! cache N (λ () (list->string (let loop ([N N][acc empty]) (define q (quotient N 10)) (define next-acc (cons (vector-ref digits (remainder N 10)) acc)) (if (zero? q) next-acc (loop q next-acc))))))))) (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**) (define iterations 100000) (define-syntax-rule (time-it number-to-convert id ...) (begin (define numbers (for/list ([i (in-range 100)]) (random number-to-convert))) (begin (collect-garbage) (time (for* ([x (in-range iterations)] [n (in-list numbers)]) (id n)))) ...)) (time-it 123 number->string number->string**) (time-it 123456 number->string number->string**) (time-it 123456789 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.