- Joined
- Nov 29, 2012
- Messages
- 240
- Reaction score
- 139
- First Language
- English
- Primarily Uses
The other one, absolutely. `eval` is extremely slow. There seems to be a worrying misconception in this community that more lines of code = slower in all cases. This is very, very false. Often, the optimum solution will be one that requires a larger amount of code. True in all cases? No. But don't just assume that short code is better. If you're curious about a particular case, benchmark it! Plenty of tools out there for that--the SES Benchmarker (found in my sig, I believe?) will work, for one.A Performance question:
I heard that eval can slow down the game if used unwise...
So, I have this little dilemma...
I got two ways to set up my method. One uses an eval method and it takes only 2 lines to do.
The other one takes 18 lines with a case statement but without any eval method.
The method will be called every single time the player attacks or does any battle actions and the battle system in question is an ABS, so the player will be mashing buttons left and right for sure to attack the enemies...
What should be better to use?
As for the other one, I haven't tested it, but I think that this should work:
def cooldown_bonus ary = [] if @note =~ /Cooldown Bonus = <(\d+[ ].*(?:\s*,\s*\d+[ ].*)*)>/i $1.split(", ").each do |info| info =~ /(\d+) (.*)/i ary << [$1.to_i, $2.downcase] # [value, stat] end end return aryendEdit for explanation: I personally prefer using `[]` to `Array.new`--no real performance difference or anything, just syntax, but it looks a little cleaner. You don't need the `to_s` for $2, since it's already a string. `map` was just returning an Enumerator there, which you weren't using--best to just go with `each`. You didn't need the counter--just append to the array, hence `<<`. Similarly, it's fine to just put both values together in an array rather than pushing them individually. I might also consider moving the Cooldown Bonus Regex to a constant or something to save the time used to build it, but that's negligble unless you're doing it a truly enormous number of times in a row, which I doubt is the case.
Last edited by a moderator:
