[Pharo-users] Dictionary removeKey: very low

PBKResearch peter at pbkresearch.co.uk
Mon Feb 3 06:24:58 EST 2020



It’s all to do with rehashing. A dictionary is stored as an Array of Association. After a key and its corresponding association is removed, the remaining entries are rehashed from the removal point to the end of the array. By doing the removals in natural order, you rehash the whole of the remaining array after each removal. Try doing the removals in reverse order; on my machine it reduces the time from 6 seconds to 6 milliseconds. 




Peter Kenny


From: Pharo-users <pharo-users-bounces at lists.pharo.org> On Behalf Of LABORDE Pierre
Sent: 03 February 2020 10:18
To: pharo-users at lists.pharo.org
Subject: [Pharo-users] Dictionary removeKey: very low


Hi all,


I have a problem with Dictionaries : 


dic := Dictionary new: 10000.


1 to: 10000 do:[ :i |

            dic at: i put: i printString.



1 to: 10000 do:[ :i |

            dic removeKey: i.      



Removing each keys is very slooow, time execution difference between add and remove is crazy.

Any idea for fix that ? Can I use another Dictionary implementation or settings ?






-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.pharo.org/pipermail/pharo-users_lists.pharo.org/attachments/20200203/f07f36ad/attachment-0001.html>

More information about the Pharo-users mailing list