Re: [Pharo-project] Issue 3533 in pharo: add Dictionary>>at:ifAbsentValue:
Updates: Summary: add Dictionary>>at:ifAbsentValue: Comment #2 on issue 3533 by stephane.ducasse: add Dictionary>>at:ifAbsentValue: http://code.google.com/p/pharo/issues/detail?id=3533 (No comment was entered for this change.)
Comment #3 on issue 3533 by stephane.ducasse: add Dictionary>>at:ifAbsentValue: http://code.google.com/p/pharo/issues/detail?id=3533 Ok wrong this was about dict at: #x ifAbsent: [nil] versus dict at: #x ifAbsent: nil
Comment #4 on issue 3533 by siguctua: add Dictionary>>at:ifAbsentValue: http://code.google.com/p/pharo/issues/detail?id=3533 Of course, you can always use: dict at: x ifAbsent: y except that it is not always desirable to send #value message to 'y' argument, especially if you can't tell anything about nature of objects you are storing in dictionary, and when speed is essential, it would be good to have something like this: at: key ifAbsentValue: defaultValue "Answer the value associated with the key or, if key isn't found, answer the default value" ^((array at: (self findElementOrNil: key)) ifNil: [ ^ defaultValue ]) value Cog VM shows a significant difference in delivering an answer: | dict | dict := Dictionary new. dict at: #a put: 1. [ 1000000 timesRepeat: [ dict at: #foo ifAbsentValue: #bar ] ] timeToRun 236 234 239 [ 1000000 timesRepeat: [ dict at: #a ifAbsentValue: #bar ] ] timeToRun 241 234 236 237 [ 1000000 timesRepeat: [ dict at: #foo ifAbsent: [#bar] ] ] timeToRun 322 318 [ 1000000 timesRepeat: [ dict at: #a ifAbsent: [ #bar ] ] ] timeToRun 363 336 340
Comment #5 on issue 3533 by rydier: add Dictionary>>at:ifAbsentValue: http://code.google.com/p/pharo/issues/detail?id=3533 "you can't tell anything about nature of objects you are storing in dictionary, and when speed is essential" Do you have any specific use caees in mind?
On Wed, 12 Jan 2011, pharo@googlecode.com wrote:
Comment #4 on issue 3533 by siguctua: add Dictionary>>at:ifAbsentValue: http://code.google.com/p/pharo/issues/detail?id=3533
Of course, you can always use:
dict at: x ifAbsent: y
except that it is not always desirable to send #value message to 'y' argument, especially if you can't tell anything about nature of objects you are storing in dictionary, and when speed is essential, it would be good to have something like this:
at: key ifAbsentValue: defaultValue "Answer the value associated with the key or, if key isn't found, answer the default value" ^((array at: (self findElementOrNil: key)) ifNil: [ ^ defaultValue ]) value
Cog VM shows a significant difference in delivering an answer:
| dict | dict := Dictionary new. dict at: #a put: 1.
[ 1000000 timesRepeat: [ dict at: #foo ifAbsentValue: #bar ] ] timeToRun 236 234 239
[ 1000000 timesRepeat: [ dict at: #a ifAbsentValue: #bar ] ] timeToRun 241 234 236 237
[ 1000000 timesRepeat: [ dict at: #foo ifAbsent: [#bar] ] ] timeToRun 322 318
[ 1000000 timesRepeat: [ dict at: #a ifAbsent: [ #bar ] ] ] timeToRun 363 336 340
Note that the block creation causes the slowdown, so if you create a block and reuse that, then there should be no measureable difference in performance. Try this: | absentBlock | absentBlock := [ #bar ]. [ 1000000 timesRepeat: [ dict at: #a ifAbsent: absentBlock ] ] timeToRun Levente
participants (2)
-
Levente Uzonyi -
pharo@googlecode.com