[resolved]technical question about hashes

Started by Poe, March 14, 2012, 04:31:05 pm

Previous topic - Next topic

Poe

March 14, 2012, 04:31:05 pm Last Edit: March 15, 2012, 12:13:05 pm by Poe
i'm looking to use the least resources for determining in what "area" (house) an event is. the best i came up with is only to check entrances and exists of houses, while the event is moving.

hash ENTRANCES holds all [x,y] of entrances as keys with the house number as value.
array EXITS holds only [x,y] of exits as its values.

now is there a speed/resource difference between:
1.for e in ENTRANCES.keys, if e == [x,y] HOME = ENTRANCES[e], then through EXIT hash setting value to 0 if found.
2.if ENTRANCE.include?([x,y]) HOUSE = ENTRANCE[x,y], elseif EXIT.include?([x,y]) HOUSE = 0.

my (fairly limited) understanding of hashes is telling me in method 2 it would determine the value same way as method 1 does. but i don't know if it really does. which might make the second slower because that would mean it checks it twice, once in include? and once in setting the value?

so is include? faster than the for loop?
is each faster than for?


also if there is a cleverer way anyone feels like sharing, i'm open to it.:p

Blizzard

March 14, 2012, 06:02:21 pm #1 Last Edit: March 14, 2012, 06:06:59 pm by Blizzard
It actually depends on the implementation of "for" and "include?". It makes somewhat sense that "include?" should be faster since it's been implemented natively in C fully while a "for" loop causes a Ruby call evaluation during each line.

Other than that, there's also the question of how keys are exactly stored. Using ".keys" will create an array of all key values first which then will be iterated while "include?" may have direct access. This is because of the nature of hash keys. You take a value, do a calculation and you already know whether the key exists or not (so-called "constant complexity" look up) while when using a "for" loop, you are restricting the available keys to be viewed as an array (so-called "n complexity" look up) which is obviously a disadvantage. In some borderline cases with very few elements, iterating through them may be faster than calculating the hash key, but these cases are obviously insignificant enough (there are just a few elements after all) to make no big difference. Whether a calculation takes 10 microseconds or 11 microseconds is really irrelevant in comparison if you're trying to keep a constant frame rate of 60 or 40.

I'd personally go with "include?" if somebody asked me (also because it's much simpler to read the code that way), but if you really want the fastest way, you'll have to run a test.

EDIT: I've seen both each and for be faster in some cases. I prefer each because you can "express yourself" better like with a loop of two variables. I'm not sure if this can be done with a "for" loop.

hash = {}
# ... filling up the hash
hash.each {|key, value|
   # do some stuff
}
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Poe