13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Concatenating StreamYou may be collecting results in a Collecting Temporary Variable. You may be collecting resultsover several methods in a Collecting Parameter.How do you concatenate several Collections?Concatenation is a simple way to join several Collections. When you have lots of Collections,though, it can be slow because the objects in the first Collection are copied once for eachCollection that is concatenated.Streams provide a way out of this dilemma. Streams are careful not to copy their contents manytimes, even as the Collection they are streaming over gets larger.Use a Stream to concatenate many Collections.I ran a quick benchmark to see how significant the difference was between Collectionconcatenation and Streams. I ran the following code, which concatenates one thousand Strings:100 timesRepeat:[result := String new.1000 timesRepeat: [result := result , 'abcdefg']]It took 53 seconds to run. Then I ran the Stream version:100 timesRepeat:[writer := WriteStream on: String new.1000 timesRepeat: [writer nextPutAll: 'abcdefg'].writer contents]]It took 0.9 seconds to run. The difference is a factor of almost 60.Sometimes you will use Concatenating Stream not because it is your choice, but because you haveto fit into other code that already uses it. The most common example is specializing object printingby overriding printOn:.<strong>Coding</strong> <strong>Patterns</strong> page 121 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!