Using Properties

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?
 
J

Jon Skeet [C# MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top