String Concatenation in Dolphin?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

String Concatenation in Dolphin?

Jan Theodore Galkowski
The message included at the bottom of this item was posted to c.l.s.
a bit ago.  Anyone have ready-made figures for Dolphin?  I'm just
asking to avoid repeating someone's already excellent work.

Thanks.  --jtg


------------posted to comp.lang.smalltalk a bit ago ----------------

Subject:
               Re: Smalltalk Tricks
         Date:
               Fri, 15 Dec 2000 01:37:49 GMT
        From:
               [hidden email]
 Organization:
               Deja.com
 Newsgroups:
               comp.lang.smalltalk
   References:
               1 , 2 , 3




The problem with string concatenation in general is that every
concatenation creates a new string object... which the garbage
collector must eventually deal with.  The concatenation of N strings
produces N-1 throw-away objects.  Streams can be more effective in some
circumstances because the stream implementation is usually smart enough
to minimize gratuitous garbage generation.  There is an overhead
associated with setting up the stream, of course, and so - as has
already been noted - it won't always speed things up.  There is also
the issue of copying the same data over and over if you are repeatedly
concatenating onto the same string... as in a program building up a
large chunk of text for a web page or such.  Here are some timings from
a VAST 5.5 image running on a relatively slow p166-class machine. The
stream version wins by a margin of 9x :

        Time millisecondsToRun:[

                | s s2 |
                s := ''.
                s2 := (String new:100) atAllPut: $.; yourself.
                1000 timesRepeat:[ s := s, s2]
        ] 7842

        Time millisecondsToRun:[

                | s s2 |
                s := WriteStream on: String new.
                s2 := (String new:100) atAllPut: $.; yourself.
                1000 timesRepeat:[  s nextPutAll: s2 ].
                s contents
        ] 83

You can also gain speed by having objects print themselves onto the
stream directly via #printOn: rather than using #printString which
allocates a string, uses #printOn: to put the data into the string, and
then answers the string.

In article <[hidden email]>,
  Christopher Painter-Wakefield <[hidden email]> wrote:

> I believe the OP was talking about the ',' message, but using a
> different notation than some of us are used to.  He had "String::,"
> instead of "String>>#,".
>
> -Christopher
>
> Chris Lopeman wrote:
> >
> > For starters I don't know what String:: is but if it is just string
> > concatination like using the ',' method then you are going to meet
a lot
> > of people who tell you this is slow.  However, the truth is it
depends
> > on your situation.  String concatination of a few number of small
> > strings or a couple of large strings will often out perform stream
use,
> > depending on how the stream is used.
> >
> > I had a program I wrote once that was very speed critical and it
had a
> > lot of string concatination of large strings. An expert came in and
we
> > agreed that this was a possible performance bottleneck.  Well after
> > replacing the code to use streams instead it got use a whopping
0.2%
> > speed improvement in the program.  Since there really was no speed
> > difference and it made the code look like horse-crap to the
programmers
> > it was decided that we should just leave it using string
concatination.
> > This was a VW 2.0 program.
> >
> > Since then I mostly use string concatination unless I think that its
> > really gonna buy me something to use a stream.  I Also try to do a
good

> > design and optimize after there is a speed problem.
> >
> > Chris Lopeman
> >
> > Object Link Inc.
> >
> > Matthew S Davis wrote:
> >
> > > Hey,
> > >
> > >      On my last ST project, we were using VAST.  I was
> > > writing a method that was doing some String concatenation
> > > using String::,  and my mentor mentioned that String::, was
> > > very slow.  He suggested instead to use a WriteStream and
> > > do WriteStream::printOn: and then to get the final string
> > > using WriteStream::asString.
> > >
> > >      Is String::, a performance problem only in VAST or
> > > is it a problem in many Smalltalk implementations?  Does
> > > anyone know of any other Smalltalk performance tricks
> > > where the "more obvious" solution runs slower than the trick?
> > >
> > >      -Hank
>


Sent via Deja.com
http://www.deja.com/



--
-----------------------------------------------------------------------
 Jan Theodore Galkowski        ºoº            [hidden email]
 algebraist.com/   wikiweb.com/    atlasti.de/      [hidden email]
***********************************************************************
 Ask me about the Disney Vacation Club, or see  algebraist.com/dvc.htm
***********************************************************************
             "Smalltalk?  Yes, it's really that slick."
-----------------------------------------------------------------------
   Want to know more?  Check out whysmalltalk.com/, object-arts.com/
***********************************************************************


Reply | Threaded
Open this post in threaded view
|

Re: String Concatenation in Dolphin?

Costas Menico-2
Jan Theodore Galkowski <[hidden email]> wrote:

<snip>

>  Here are some timings from
>a VAST 5.5 image running on a relatively slow p166-class machine. The
>stream version wins by a margin of 9x :
>
>        Time millisecondsToRun:[
>
>                | s s2 |
>                s := ''.
>                s2 := (String new:100) atAllPut: $.; yourself.
>                1000 timesRepeat:[ s := s, s2]
>        ] 7842

I get with D4.0 on my 266Mhz machine: 896

>
>        Time millisecondsToRun:[
>
>                | s s2 |
>                s := WriteStream on: String new.
>                s2 := (String new:100) atAllPut: $.; yourself.
>                1000 timesRepeat:[  s nextPutAll: s2 ].
>                s contents
>        ] 83

I get with D4.0 on my 266Mhz machine: 75

It seems the ration using Dolphin maybe smaller than vast.

Costas


Reply | Threaded
Open this post in threaded view
|

Re: String Concatenation in Dolphin?

Chris Uppal-2
Costas Menico wrote:

>
> >        Time millisecondsToRun:[
> >
> >                | s s2 |
> >                s := ''.
> >                s2 := (String new:100) atAllPut: $.; yourself.
> >                1000 timesRepeat:[ s := s, s2]
> >        ] 7842
>
> I get with D4.0 on my 266Mhz machine: 896
>
> >
> >        Time millisecondsToRun:[
> >
> >                | s s2 |
> >                s := WriteStream on: String new.
> >                s2 := (String new:100) atAllPut: $.; yourself.
> >                1000 timesRepeat:[  s nextPutAll: s2 ].
> >                s contents
> >        ] 83
>
> I get with D4.0 on my 266Mhz machine: 75

That's *very* odd.  On this 266 P1, 96MByte, Win98 sp1, Dolphin 4. I get
times of 2260 msec and 19 msec respectively.  That's a very big difference,
both in ratio and absolute time, from what you are seeing.

(The same test on a 650 PIII, gives times in about the same 100/1 ratio, but
about 4 times faster absolute)

    -- chris


Reply | Threaded
Open this post in threaded view
|

Re: String Concatenation in Dolphin?

Costas Menico-2
In reply to this post by Jan Theodore Galkowski
>
>        Time millisecondsToRun:[
>
>                | s s2 |
>                s := ''.
>                s2 := (String new:100) atAllPut: $.; yourself.
>                1000 timesRepeat:[ s := s, s2]
>        ]
>
>        Time millisecondsToRun:[
>
>                | s s2 |
>                s := WriteStream on: String new.
>                s2 := (String new:100) atAllPut: $.; yourself.
>                1000 timesRepeat:[  s nextPutAll: s2 ].
>                s contents
>        ]

Sorry I messed up.

After evaluating the above in a workspace a few times I get an average
of 870 and 13 millisecs.

I am running with D4.0 PL1, 233Mhz, Windows 95 and 96MB RAM

Costas