On Thu, 11 Mar 2010, Stéphane Ducasse wrote:



So try it with randomized data:

a := (1 to: 1000000) asArray shuffled.

Smalltalk garbageCollect.
[ a collect: #yourself as: Set ] timeToRun. "==> 4143"

Smalltalk garbageCollect.
[ a asSet ] timeToRun. "==> 3995".

| s |
s := Set new.
Smalltalk garbageCollect.
[ s addAll: a ] timeToRun. "==> 157304 oops weak hashes".

| s |
s := Set new: 1000000.
Smalltalk garbageCollect.
[ s addAll: a ] timeToRun. "==> 636".


With better hashes:

levente how do you determine that you use better hashes below

The hash of a SmallInteger is itself. The larger the interval where the hashes come from the better, because the distribution of (hash \\ array size) values is more uniform, therefore the chance of clustering and collisions is lower.


Levente



a := Set new: 1000000.
[ a size < 1000000 ] whileTrue: [
        a add: SmallInteger maxVal atRandom ].
a := a asArray.

Smalltalk garbageCollect.
[ a collect: #yourself as: Set ] timeToRun. "==>  3895"

Smalltalk garbageCollect.
[ a asSet ] timeToRun. "==>  3777".

| s |
s := Set new.
Smalltalk garbageCollect.
[ s addAll: a ] timeToRun. "==> 4729 much better".

| s |
s := Set new: 1000000.
Smalltalk garbageCollect.
[ s addAll: a ] timeToRun. "==> 774".


Conclusions:
- #collect:as: is (almost) as fast as #asSet
- better hashes can make a huge difference
- message sends cost a lot compared to bytecodes
- if you can guess the size of your collection, do it (applies for all growing 
collections and collection streaming)
yes!!
- only optimize if you have to in your own code


Levente

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<mer...@stonehenge.com> <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

_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project


_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project
_______________________________________________
Pharo-project mailing list
Pharo-project@lists.gforge.inria.fr
http://lists.gforge.inria.fr/cgi-bin/mailman/listinfo/pharo-project

Reply via email to