Using Properties

  • Thread starter Thread starter Devin Dow
  • Start date Start date
D

Devin Dow

Should I be storing a property in a temporary variable for performance reasons?

Example:
Assume MyClass has a Property called Center that is CPU intensive to calculate.

Would this:

if (MyClass.Center.x > 0 || MyClass.Center.y > 0) ...

take twice as long as this:

Point temp = MyClass.Center;
if (temp.x > 0 || temp.y > 0) ...

or does to compiler take care of this situation?
 
Devin Dow said:
Should I be storing a property in a temporary variable for performance reasons?

Not unless you've already established that this is your performance
bottleneck - which it's unlikely to be.

Yes, the property will be called twice. However, it's likely to be
inlined and the performance hit will be miniscule - but your code ends
up more readable (IMO). Optimisation like this can and should be left
until later.
 
Back
Top