unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Daniel Hartwig <mandyke@gmail.com>
To: guile-devel@gnu.org
Subject: Re: Extremly slow for format & string-join
Date: Mon, 1 Apr 2013 15:40:48 +0800	[thread overview]
Message-ID: <CAN3veRdpuUEbPEX5=G9WP6Hpkwad5-Hp7CQXhJxdh+PROOP3qg@mail.gmail.com> (raw)
In-Reply-To: <E31E42E6-4A7F-484B-8DD5-7CA76C9F8982@bluewin.ch>

On 1 April 2013 14:59, Daniel Llorens <daniel.llorens@bluewin.ch> wrote:
>
> Hello,
>
>> From: Daniel Hartwig <mandyke@gmail.com>
>>
>> (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?
>
> I attempted a method that I thought should surely be faster using
> https://gitorious.org/guile-ploy
>
> (import (util ploy))
> (define str*-as-array (lambda (s n) (ravel (reshape s n (string-length s)))))
>
> ravel is essentially
>
> (define (ravel a)
>   (or (array-contents a) (array-contents (array-copy (array-type a) a))))
>
>
> reshape is more complicated but in this particular case it resolves
> to make-shared-array, so it's O(1).
>
> Here's a full trace:
>
> scheme@(guile-user)> ,trace (string-length (str*-as-array "1234567890" 1000000))

>
> It is in fact quite slower than your solution using call-with-output-string + display:
>
> scheme@(guile-user)> ,time (string-length (str* "1234567890" 1000000))
> $4 = 10000000
> ;; 0.528000s real time, 0.530000s run time.  0.000000s spent in GC.
> scheme@(guile-user)> ,time (string-length (str*-as-array "1234567890" 1000000))
> $5 = 10000000
> ;; 1.745000s real time, 1.750000s run time.  0.000000s spent in GC.
> scheme@(guile-user)>
>
> The profile is interesting, I think:
>
> scheme@(guile-user)> ,profile (string-length (str*-as-array "1234567890" 1000000))
> %     cumulative   self
> time   seconds     seconds      name
> 100.00      1.74      1.74  make-typed-array
>   0.00      1.74      0.00  call-with-prompt
>   0.00      1.74      0.00  start-repl
>   0.00      1.74      0.00  catch
>   0.00      1.74      0.00  #<procedure 1161a37c0 at ice-9/top-repl.scm:31:6 (thunk)>
>   0.00      1.74      0.00  apply-smob/1
>   0.00      1.74      0.00  run-repl
>   0.00      1.74      0.00  statprof
>   0.00      1.74      0.00  array-copy
>   0.00      1.74      0.00  #<procedure 117762d80 at statprof.scm:655:4 ()>
>   0.00      1.74      0.00  #<procedure 117b05e80 at <current input>:5:0 ()>
>   0.00      1.74      0.00  ravel
>   0.00      1.74      0.00  #<procedure 1161a36c0 at ice-9/top-repl.scm:66:5 ()>
>
> How can it be slower to allocate the result at once?
>

Shrug.  I do not know much of array internals.  You probably have much
more experience there than I.

Except for the curious profile output, I suspect the overhead is due
to such factors as repeated application of MAPFUNC and consequent
arithmetic to access the shared arrays contents

I see no reason to expect O(1) allocation of storage to be a
significant factor here.  I have not checked, but suspect that
‘call-with-output-string’ is very efficient with its storage
allocation.  Of course, comparing either of these to the
original implementations using ‘string-join’ and ‘format’ I
certainly would expect the allocation performance to be
significant.



  reply	other threads:[~2013-04-01  7:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.1257260.1364793213.854.guile-devel@gnu.org>
2013-04-01  6:59 ` Extremly slow for format & string-join Daniel Llorens
2013-04-01  7:40   ` Daniel Hartwig [this message]
2013-04-01  4:00 Nala Ginrut
2013-04-01  4:39 ` Daniel Hartwig
2013-04-01  5:13   ` Nala Ginrut
2013-04-01  5:35     ` Daniel Hartwig
2013-04-01  6:58       ` Nala Ginrut
2013-04-01  7:02         ` Daniel Hartwig
2013-04-01  8:36 ` Mark H Weaver
2013-04-01  9:52   ` Nala Ginrut
2013-04-01 12:55     ` Ian Price
2013-04-02 15:56   ` Ludovic Courtès
2013-04-01 10:37 ` Thien-Thi Nguyen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAN3veRdpuUEbPEX5=G9WP6Hpkwad5-Hp7CQXhJxdh+PROOP3qg@mail.gmail.com' \
    --to=mandyke@gmail.com \
    --cc=guile-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).