C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Method1: The first version, Method1, uses five local variables to store the intermediate results of this computation.
Method2: This combines the arithmetic into a single expression. The results of the program show that Method2 is somewhat faster.
C# program that measures performance
using System;
using System.Diagnostics;
class Program
{
static int _temp1 = 5;
static int _temp2 = 10;
static int Method1()
{
// Use local variables to perform computation.
int a = _temp1;
int b = a * 10;
int c = b * _temp2;
int d = c + 5;
int e = d - _temp2;
return e * 2;
}
static int Method2()
{
// Use expression to perform computation.
return ((_temp1 * 10 * _temp2) + 5 - _temp2) * 2;
}
const int _max = 300000000;
static void Main()
{
// Test.
Console.WriteLine(Method1());
Console.WriteLine(Method2());
// Clean up environment.
GC.Collect();
System.Threading.Thread.Sleep(10);
for (int a = 0; a < 10; a++)
{
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method1();
}
s1.Stop();
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
Method2();
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00") + " ns");
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000 * 1000) /
_max).ToString("0.00") + " ns");
}
Console.Read();
}
}
Output
990
990
2.32 ns
1.92 ns
2.53 ns
2.10 ns
2.40 ns
1.91 ns
2.31 ns
1.91 ns
2.31 ns
1.91 ns
2.31 ns
1.92 ns
2.32 ns
1.92 ns
2.31 ns
1.91 ns
2.32 ns
1.91 ns
2.36 ns
1.91 ns
However: Method2 uses the evaluation stack more directly. It does not allocate local variable memory and store values in those slots.
Note: Please retest this on different versions of the .NET Framework if you are in doubt.