There is no difference in your 2 code examples, both would run at the same speed.
Is there a good rule of thumb?
Yes:
Do not attempt to over-optimize your code.
I follow these basic rules:
Avoid square root operations, and when required, store the result if needed multiple times.
Avoid *
, /
or %
by powers of 2. Use <<
, >>
and &
instead.
Use bit fields as much as possible. Parallel condition checks become "all for the cost of one".
Try to avoid nested loops, sometimes unavoidable, but if you hit a 3rd or ouch 4th nesting level, consider other ways to accomplish the task, try a tree, grid or other data structure.
Always break;
or return
out of loops early if possible.
Avoid recursion, unless the bounds are known and/or maxdepth is provided.
If you find yourself reusing the same heap reference a bunch of times: "object.something" or "object->something" store it in a local variable to help with cache locality.
Like every rule, these were made to be broken with discretion.
Beyond these rules, I ask myself: "Is it worth the hassle?" The profiler times will usually answer this question and identify the critical sections for me.
I only then do I optimize the problem areas.
To address your "vtable" concern, use virtual
sparingly, or simply avoid inheritance in your classes and use static "POD" calls with reference parameters to implement common methods.
This is similar to what the compiler does in the background, only avoiding the run-time lookup table since the classes and calls are concrete(fixed addresses are provided) at compile time.
That is overkill, but to eek out every ounce of performance, this answers your question as asked.
Vector2f
wouldn't be using vtables. I can post an answer explaining why, but I wanted to make sure I wasn't missing something. \$\endgroup\$