[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Extremly slow for format & string-join
From: |
Daniel Hartwig |
Subject: |
Re: Extremly slow for format & string-join |
Date: |
Mon, 1 Apr 2013 12:39:02 +0800 |
2013/4/1 Nala Ginrut <address@hidden>:
> I've tried to implement a function to mimic string multiply like Python:
> "asdf" * 10
>
> --------------code----------------
> (define (str* str n)
> (format #f "~{~a~}" (make-list n str)))
>
> or
>
> (define (str* str n)
> (string-join (make-list n str) ""))
> --------------end-----------------
>
>
Those are both very general mechanisms, it does not suprise me that
they are less than efficient for very large N. Although I can not
comment whether this is a worthwhile issue to address, I offer this
snippet as a hint of something perhaps better for your specific case:
(define (str* str n)
(call-with-output-string
(lambda (p)
(let lp ((n n))
(unless (zero? n)
(display str p)
(lp (1- n)))))))
Out of curiousity, how does the performance figures you showed compare
to the Python operator for similarly large values of N?
Re: Extremly slow for format & string-join, Mark H Weaver, 2013/04/01
Re: Extremly slow for format & string-join, Thien-Thi Nguyen, 2013/04/01