[MacRuby-devel] Question on Hash performance

Caio Chassot lists at caiochassot.com
Thu Jan 13 23:45:19 PST 2011


I'm not perfectly sure this applies to MacRuby, but I suspect it should since our Array is implemented on top of NSMutableArray:

http://ridiculousfish.com/blog/archives/2005/12/23/array/

But the gist is CFArrays more or less switch to using hashes internally at some point.


On 2011-01-14, at 05:05 , Robert Rice wrote:
> 
> I suspect that there is considerably more overhead in creating a Hash  as compared to an Array and that using an Array is more efficient unless the Array will become large. I am wondering about how large the Array would have to be before it becomes more efficient to use a Hash. Consider the following example:
> 
> class Test
> 	def initialize
> 		@array	= []
> 		@hash	= {}
> 	end
> 
> 	def get_from_hash( key )
> 		@hash[ key ]
> 	end
> 
> 	def set_hash( key, value )
> 		value.nil? ? @hash.delete( key ) : @hash[ key ] = value
> 	end
> 
> 	def get_from_array( key )
> 		pair = @array.assoc( key )
> 		pair ? pair.last : nil
> 	end
> 
> 	def set_array( key, value )
> 		pair = @array.assoc( key )
> 		if pair
> 			value.nil? ? @array.delete( pair ) : pair[1] = value
> 			else
> 			@array << [ key, value ] unless value.nil?
> 		end
> 	end
> 
> end
> 
> Clearly it requires more code to emulate the hash function with an Array, but would it run faster for a small number of keys?
> 
> Bob Rice
> 
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel




More information about the MacRuby-devel mailing list