Hello,
Im trying to concatenate many collections (sets) together. I learn (from becks book) that the use of stream is efficient so I try the following code
write _ WriteStream on: Set new.
write nextPutAll: (Set with:4 with:5)
write contents
but it doesnt work because Set isnt a member of a SequenceableCollection then it doesnt understand the message : replaceFrom:to:with:startingAt:
Finally I try a code as:
write _ WriteStream on: {}.
write nextPutAll: (Set with:4 with:5)
write contents asSet
Im happy with the result but I need just to know if there is another pattern to use Set and Stream.
Thanks,
Houssam
____
"Le paradis touche les pieds des mères..."
Houssam Fakih wrote:
Hello,
I’m trying to concatenate many collections (sets) together. I learn (from beck’s book) that the use of stream is efficient so I try the following code
write _ WriteStream on: Set new.
write nextPutAll: (Set with:4 with:5)
write contents
but it doesn’t work because Set isn’t a member of a SequenceableCollection then it doesn’t understand the message : replaceFrom:to:with:startingAt:
Finally I try a code as:
write _ WriteStream on: {}.
write nextPutAll: (Set with:4 with:5)
write contents asSet
I’m happy with the result but I need just to know if there is another pattern to use Set and Stream.
I'm not entirely sure what you're trying to do, but:
a := Set with: 1 with: 'a' with: #hello. b := Set with: 2 with: 'b' with: #world. c := Set new. c addAll: a. c addAll: b.
Is that what you want? Remember to look at the Set class and see what it can do.
Mikevdg.
Hi,
-----Message d'origine----- De : squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] De la part de Michael van der Gulik Envoyé : samedi 30 juillet 2005 02:09 À : squeak-dev@lists.squeakfoundation.org Objet : Re: set and stream
Houssam Fakih wrote:
Hello,
Im trying to concatenate many collections (sets) together. I learn (from becks book) that the use of stream is efficient so I try the following code
write _ WriteStream on: Set new.
write nextPutAll: (Set with:4 with:5)
write contents
but it doesnt work because Set isnt a member of a SequenceableCollection then it doesnt understand the message : replaceFrom:to:with:startingAt:
Finally I try a code as:
write _ WriteStream on: {}.
write nextPutAll: (Set with:4 with:5)
write contents asSet
Im happy with the result but I need just to know if there is another pattern to use Set and Stream.
I'm not entirely sure what you're trying to do, but:
a := Set with: 1 with: 'a' with: #hello. b := Set with: 2 with: 'b' with: #world. c := Set new. c addAll: a. c addAll: b.
Is that what you want? Remember to look at the Set class and see what it can do.
[Fakih]
Thank you for your response. What I need is a method that does the same computation as addAll: with better performance. By using streams I improve the performance by 70%. My question was just to know if the developers on the list use another pattern (other than using streams) to improve that.
Thanks, Houssam
Mikevdg.
"Houssam Fakih" fakih@ensm-douai.fr wrote:
Thank you for your response. What I need is a method that does the same computation as addAll: with better performance. By using streams I improve the performance by 70%. My question was just to know if the developers on the list use another pattern (other than using streams) to improve that.
addAll: is not that bad. It is certainly better than concatenation with the #, method. The problem with repeated concatenation is that increasingly large collections are copied again and again. This is avoided both with #addAll: and with a stream (for collection that a stream can write into).
Please compare these two examples:
" repeated concatenation " |sets union | sets := OrderedCollection new. 1 to: 100 do: [:idx | sets add: (Set with: idx)]. Time millisecondsToRun: [ 1000 timesRepeat: [union := sets first. 2 to: 100 do: [:idx | union := union, (sets at: idx)]. 1 to: 100 do: [:idx | union := union, (sets at: idx)]. ].].
" use of #addAll: " |sets union | sets := OrderedCollection new. 1 to: 100 do: [:idx | sets add: (Set with: idx)]. Time millisecondsToRun: [ 1000 timesRepeat: [union := Set new:100. 1 to: 100 do: [:idx | union addAll: (sets at: idx) ]. 1 to: 100 do: [:idx | union addAll: (sets at: idx)]. ].].
Additional notes:
1. When you can guess the size of the union set, you should use that guess to create an empty set of a suitable capacity. With a set of suitable capacity you avoid some grow operations.
2. Keep in mind that insertion into a set is costly when most set elements a mapped onto only a few hash values. This is sometimes a really serious problem.
Greetings Boris
-----Message d'origine----- De : squeak-dev-bounces@lists.squeakfoundation.org [mailto:squeak-dev- bounces@lists.squeakfoundation.org] De la part de Boris Gaertner
addAll: is not that bad. It is certainly better than concatenation with the #, method. The problem with repeated concatenation is that increasingly large collections are copied again and again. This is avoided both with #addAll: and with a stream (for collection that a stream can write into).
[Fakih] I compare the code you sent to me with streams. #addAll is the method with the high performance comparing to the concatenation #, (+48.57 %), and streams (+7.265%).
" use of #streams " |sets union stream| sets := OrderedCollection new. stream _ WriteStream on: {}. 1 to: 100 do: [:idx | sets add: (Set with: idx)]. Time millisecondsToRun: [ 1000 timesRepeat: [1 to: 100 do: [:idx | stream nextPutAll: (sets at: idx)]. 1 to: 100 do: [:idx | stream nextPutAll: (sets at: idx)]. ].union _ Set withAll: stream contents.].
Nevertheless if I test the following code, using streams is more performed than #addAll: (+20.2 %) as well as than concatenation #, (+78.28):
|result time stream| result _ Set new. time _ Time millisecondsToRun: [ 1 to: 10000 do: [: index | result _ result, (Set with: ('a', index asString)) ]]. Transcript cr; show: 'Concatenate Perf' ; show: time.
time _ 0. result _ Set new. time _ Time millisecondsToRun: [ 1 to: 10000 do: [: index | result addAll: (Set with: ('a', index asString)) ]]. Transcript cr; show: 'AddAll Perf' ; show: time.
time _ 0. result _ Set new. stream _ WriteStream on: {}. time _ Time millisecondsToRun: [ 1 to: 10000 do: [: index | stream nextPutAll: (Set with: ('a', index asString)) ]]. Transcript cr; show: 'Stream Perf' ; show: time ; cr ; cr.
I can't see how I can decide between #addAll and #nextPutAll in case of sets? In case of arrays to concatenate I'm convinced that streams are more efficient.
Cheers, Houssam
squeak-dev@lists.squeakfoundation.org