views:

31

answers:

1

Is there any performance difference between:

size.width += this->font->chars[*ch].advance + this->font->chars[*ch].offset.x;

and

char_data *chars = this->font->chars;
while(...) {
   size.width += chars[*ch].advance + chars[*ch].offset.x;
}

In first example are always read vars( this->font, font->chars ) within loop, or they are cached?

+3  A: 

That would depend on your compiler and optimization settings. At the most basic level, the first one will be slower because you're doing extra dereferencing and access operations. But in reality, the optimizer can identify these repetitions and eliminate them.

To definitively answer this, you should run a test and compare the two to see if there is a statistically significant difference in running times.

JoshD
Would the optimizer do this? Who's to say I'm not changing `font` in another thread?
John at CashCommons
@John at CashCommons: Nothing. That's why you need to use mutexes if you have two threads accessing the same data with one writing to it. If you expect this to happen, declare it volatile (I think that's the one) and it won't do this optimization.
JoshD