appending Strings

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

appending Strings

Mark Volkmann
Is there a reason to prefer one of these approaches over the other?
Maybe one is more efficient and should be preferred if doing a large  
number of appends.

Approach #1

s := ''.
s := s, 'foo'.
s := s, 'bar'

Approach #2

stream := WriteStream on: ''.
stream nextPutAll: 'foo'.
stream nextPutAll: 'bar'.
s := stream contents

Are there other approaches I should consider?

---
Mark Volkmann




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Randal L. Schwartz
>>>>> "Mark" == Mark Volkmann <[hidden email]> writes:

Mark> stream := WriteStream on: ''.
Mark> stream nextPutAll: 'foo'.
Mark> stream nextPutAll: 'bar'.
Mark> s := stream contents

s := String streamContents: [:stream |
  stream
        nextPutAll: 'foo';
        nextPutAll: 'bar'.
].

Then you don't even have to name the stream, because it has that
temporary name inside the block.

Look for the many senders of #streamContents: as examples.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Herbert König
In reply to this post by Mark Volkmann
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mariano Abel Coca
Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mark Volkmann
That reminds me. I know I ran across a method that runs a block and then tells you how long it took. Anyone remember what that's called?

On Oct 3, 2008, at 3:09 PM, Mariano Abel Coca wrote:

Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mariano Abel Coca
I think you must be thinking in #timeToRun.

About that... In VisualAge there are the EsbTracer and EsbSampler, which allows you to meassure the time that involves every message sent. It's useful to tune some code... But don't know in Squeak. Any ideas?

Cheers,

--
Mariano.

On Fri, Oct 3, 2008 at 5:44 PM, Mark Volkmann <[hidden email]> wrote:
That reminds me. I know I ran across a method that runs a block and then tells you how long it took. Anyone remember what that's called?

On Oct 3, 2008, at 3:09 PM, Mariano Abel Coca wrote:

Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners







_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mark Volkmann
On Oct 3, 2008, at 3:59 PM, Mariano Abel Coca wrote:

I think you must be thinking in #timeToRun.

No, but you saying that reminded me of where I saw it. It's in the Time class.

ms := Time millisecondsToRun: [ do some stuff here ]

Using this on the code below, the comma approach took 8478 milliseconds and the stream approach took 185 milliseconds. Big difference!

About that... In VisualAge there are the EsbTracer and EsbSampler, which allows you to meassure the time that involves every message sent. It's useful to tune some code... But don't know in Squeak. Any ideas?

Cheers,

--
Mariano.

On Fri, Oct 3, 2008 at 5:44 PM, Mark Volkmann <[hidden email]> wrote:
That reminds me. I know I ran across a method that runs a block and then tells you how long it took. Anyone remember what that's called?

On Oct 3, 2008, at 3:09 PM, Mariano Abel Coca wrote:

Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners






_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Bert Freudenberg

Am 03.10.2008 um 14:13 schrieb Mark Volkmann:

> On Oct 3, 2008, at 3:59 PM, Mariano Abel Coca wrote:
>
>> I think you must be thinking in #timeToRun.
>
> No, but you saying that reminded me of where I saw it. It's in the  
> Time class.
>
> ms := Time millisecondsToRun: [ do some stuff here ]
>

which is the same as

ms := [ do some stuff here ] timeToRun.

- Bert -


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mariano Abel Coca
In reply to this post by Mark Volkmann

Actually, the definition of timeToRun in BlockContext is:

BlockContext>>timeToRun
    "Answer the number of milliseconds taken to execute this block."

    ^ Time millisecondsToRun: self


So, your example it's the same than:

[ do some stuff here ] timeToRun

Remember: let the objects do the work for you :-)


On Fri, Oct 3, 2008 at 6:13 PM, Mark Volkmann <[hidden email]> wrote:
On Oct 3, 2008, at 3:59 PM, Mariano Abel Coca wrote:

I think you must be thinking in #timeToRun.

No, but you saying that reminded me of where I saw it. It's in the Time class.

ms := Time millisecondsToRun: [ do some stuff here ]

Using this on the code below, the comma approach took 8478 milliseconds and the stream approach took 185 milliseconds. Big difference!
 
 
Nice... I've never measured it ;)
 

About that... In VisualAge there are the EsbTracer and EsbSampler, which allows you to meassure the time that involves every message sent. It's useful to tune some code... But don't know in Squeak. Any ideas?

Cheers,

--
Mariano.

On Fri, Oct 3, 2008 at 5:44 PM, Mark Volkmann <[hidden email]> wrote:
That reminds me. I know I ran across a method that runs a block and then tells you how long it took. Anyone remember what that's called?

On Oct 3, 2008, at 3:09 PM, Mariano Abel Coca wrote:

Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners






_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners




--
Mariano.
www.egola.com.ar

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Mark Volkmann
Thanks to all! Another thing to add to my growing set of notes.

On Oct 3, 2008, at 4:41 PM, Mariano Abel Coca wrote:


Actually, the definition of timeToRun in BlockContext is:

BlockContext>>timeToRun
    "Answer the number of milliseconds taken to execute this block."

    ^ Time millisecondsToRun: self


So, your example it's the same than:

[ do some stuff here ] timeToRun

Remember: let the objects do the work for you :-)


On Fri, Oct 3, 2008 at 6:13 PM, Mark Volkmann <[hidden email]> wrote:
On Oct 3, 2008, at 3:59 PM, Mariano Abel Coca wrote:

I think you must be thinking in #timeToRun.

No, but you saying that reminded me of where I saw it. It's in the Time class.

ms := Time millisecondsToRun: [ do some stuff here ]

Using this on the code below, the comma approach took 8478 milliseconds and the stream approach took 185 milliseconds. Big difference!
 
 
Nice... I've never measured it ;)
 

About that... In VisualAge there are the EsbTracer and EsbSampler, which allows you to meassure the time that involves every message sent. It's useful to tune some code... But don't know in Squeak. Any ideas?

Cheers,

--
Mariano.

On Fri, Oct 3, 2008 at 5:44 PM, Mark Volkmann <[hidden email]> wrote:
That reminds me. I know I ran across a method that runs a block and then tells you how long it took. Anyone remember what that's called?

On Oct 3, 2008, at 3:09 PM, Mariano Abel Coca wrote:

Just check it by yourself. ;)

|result|
100 timesRepeat:
[ result := String new.
4000 timesRepeat: [ result := result, 'abcdefg' ]].
^result


|writer|
100 timesRepeat:
[ writer := WriteStream on: String new.
4000 timesRepeat: [ writer nextPutAll:'abcdefg' ]].
^writer contents

Cheers,

--
Mariano.


On Fri, Oct 3, 2008 at 2:02 PM, Herbert König <[hidden email]> wrote:
Hello Mark,

Friday, October 3, 2008, 4:52:39 PM, you wrote:

MV> Is there a reason to prefer one of these approaches over the other?
MV> Maybe one is more efficient and should be preferred if doing a large
MV> number of appends.

speed is the reason for the stream approach. The #, approach copies
the first string once for each #, send.


MV> Are there other approaches I should consider?

Don't know of any but then I'm not an expert.


Cheers,

Herbert

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners






_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners




--
Mariano.
www.egola.com.ar
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


---
Mark Volkmann





_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Aidan Gauland
Or you can pick "tally it" from the menu in a waorkspace, instead of "do it".

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: appending Strings

Herbert König
Hello Mark,

Saturday, October 4, 2008, 8:17:46 AM, Aidan wrote:

AG> Or you can pick "tally it" from the menu in a waorkspace, instead of "do it".

to be a bit more explicit, you might want to look at MessageTally's
class side #tallySends and TimeProfileBrowser #spyOn: and TheWorldMenu
#startThenBrowseMessageTally. The latter is what gets invoked from the
World menu, debug start/browse MessageTally.

To understand what you see there please search the archives (of Squeak
dev) this is a FAQ.

BTW #tallySends IMHO uses the simulator, you would not want to use it
on the examples of Mariano due to its looow speed.

--
Cheers,

Herbert  

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners