That I do not know for sure. I did test that a 140 relatively simple if-checks per frame take 0.001 second (on a super fast pc). So about a 0.06 FPS drain in worst case and perhaps an estimated 0.2 FPS drain for a much older PC. 0.2 isn't much but it's still a huge waste for a bunch of inactive scripts :/. And I did not even alias.
I already have that (sorry wasn't clear I typed the sample wrong):
Only if my_stuff is not nil, only then is it updated. "my_stuff" could be anything in this sample.
But sometimes you can't resort to a simple if-statement and you have to check several variables/states to check if the script should be active. This can often be optimized though.
But it seems that everyone just goes with the if-check instead of using a different parent class or something. But it just isn't too great for large projects.
Wait, what?
I'm not quite sure your numbers are accurate. I ran a benchmark over
ten million iterations using a simple check from a local variable:
require 'benchmark'i = 10000000t = truef = falsen = nil other = "purple"Benchmark.bmbm do |x| x.report { i.times { n if t } } # true test x.report { i.times { n if f } } # false test x.report { i.times { n if other } } # implicit true test x.report { i.times { } } # empty blockendWith the following results:
Rehearsal ------------------------------------ 0.749000 0.000000 0.749000 ( 0.736042) 0.733000 0.000000 0.733000 ( 0.735043) 0.733000 0.000000 0.733000 ( 0.738042) 0.656000 0.000000 0.656000 ( 0.649037)--------------------------- total: 2.871000sec user system total real 0.733000 0.000000 0.733000 ( 0.732042) 0.686000 0.031000 0.717000 ( 0.721041) 0.733000 0.000000 0.733000 ( 0.731042) 0.656000 0.000000 0.656000 ( 0.658038)My computer is pretty fast, but that is several orders of magnitude off your estimates. And as you can see, the bulk of that benchmark time is just from
iterating the block used to test even the empty block, the 'if' test itself is a further order of magnitude off from that. The 'if' check itself will have very little bearing at all. If you use a single comparative, you'll get similar results, even if it has to go through a couple accessors to get there. This is
not likely to become your bottleneck, especially if it is the update method for Game_Map, which is only run once every frame, considering everything else that goes in in a frame update.
Your main concern with branching, rather than performance, is that it reduces readability of your code and therefore makes it harder to trace and debug. So multiple 'if' statements that have two branches that do drastically different things are generally not a good idea for this reason, though this is usually fine if you separate the two branches into different methods.
Ideally, if you wanted to do such a thing, you would use a separate class here, or singleton methods, but it just isn't feasible in this case. Other classes are reliant on the global instance $game_map to do what they need to do, so subclassing and replacing the variable may cause you issues elsewhere. The logical point to perform the switch between the two classes is also the setup method of Game_Map - or every single method that calls it. You would then have to copy over any instance variables that have a persistent state between the two objects (ie., the interpreter and screen). This would then make your code less intuitive from an outside perspective because it would then be unclear which class $game_map was at any given time, and which one ends up getting serialized when the game is saved.
Basically, if you wanted to do either of these with a Scene at point of call, it would be perfectly fine, because only the Scene itself really cares what is going on. But I would avoid doing this for any of the global instances.
Since I don't really see how you would Game_Map (which basically a persistent container for a bunch of variables with a few internal calculations) enough that its function would no longer be clear, you should probably just branch inside the method(s) like normal. If you just need to branch inside the update method to do some additional stuff, it will be perfectly fine both performance wise and readability wise. It only becomes bad if you need to conditionally branch
multiple methods and make them do
different stuff within these same methods, at which point you should rethink your implementation.
I've seen my fair share of scripts with performance issues. Excessive sprite use or bitmap operations, redrawing window contents every frame, repeatedly performing string matches instead from notes instead of caching the results, greatly increasing the length of a method that is called hundreds of times per frame, and/or excessive iteration (usually combined with one or more of the others) are all things that can cause performance issues. An 'if' statement or two and a couple comparatives with method calls that return a variable called one time per frame is not going to hurt you.