Thursday, February 21, 2008

[.NET] Built-in Types Performance Tip

Here's something not everyone knows about .NET framwork built-in types performance:
Quoting from MCTS Training Kit for Exam 70-536, .NET Framework 2.0 - Application Development foundation:

"The runtime optimizes the performance of 32-bit integer types (Int32 and UInt32), so use those types for counters and other frequently accessed integral variables. For floating-point operations, Double is the most efficient type because those operations are optimized by hardware."

Let's be honest: in average development conditions this won't make much of a difference. Depending on what kind of app your working on, tough, it means that -if you're not working on very constrictive memory conditions- even if you need just a float (4 bytes) to represent numeric values, you should go for a double in order to optimize your floating point operations, and so on.

It's just kind of nice to know this, so next time you'll meet a memory maniac -who's using short variables as counters- you'll have a concrete reason to laugh at him.

1 comment:

Sam said...

Shorts as counters are slower than int. I've tried :)

But comparing against zero is a good boost.