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
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@lists.macosforge.org http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel
Hi,
I'm not perfectly sure this applies to MacRuby, but I suspect it should since our Array is implemented on top of NSMutableArray
MacRuby's Array was based on CFArray at some point but it's not anymore. If I remember correctly there were a few reasons for it, including : - CFArray's performance characteristics are very different from the standard Ruby's array - we had to do some boxing/unboxing for simple types (integers, booleans) Note that if you want CFArray's performance characteristics you can still use NSArray in MacRuby.
participants (3)
-
Caio Chassot
-
Robert Rice
-
Vincent Isambart