D
Dave Harris
In theory all three loops should operate at the same speed.
Loop1 is the fastest by quite a bit,
Loop2 is the slowest
Loop3 is somewhat faster than Loop2.
This is built in production with the optimizer on 32 and 64 bit code. If
I force the functions to not inline then the speed is identical, so best I
can figure is that the JIT somehow makes different inlining decisions. This
is more of a curiosity than anything else, I wouldn't base coding decisions
on this finding, but still it would be interesting to know what's up.
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
public class M
{
public static bool IsZero(double d1)
{
return (-.000001 < d1 && d1 < .000001);
}
public static bool IsZero2(double d1)
{
return (d1 > -.000001 && d1 < .000001);
}
public static bool IsZero3(double d1)
{
return (d1 > -.000001 && .000001 > d1);
}
static public void Main()
{
double sum;
{
Stopwatch st = new Stopwatch();
st.Start();
Console.WriteLine("Ignore - make sure any initialization needed in this
class is done");
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero2(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero3(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
}
}
}
Loop1 is the fastest by quite a bit,
Loop2 is the slowest
Loop3 is somewhat faster than Loop2.
This is built in production with the optimizer on 32 and 64 bit code. If
I force the functions to not inline then the speed is identical, so best I
can figure is that the JIT somehow makes different inlining decisions. This
is more of a curiosity than anything else, I wouldn't base coding decisions
on this finding, but still it would be interesting to know what's up.
using System;
using System.Diagnostics;
namespace ConsoleApplication1
{
public class M
{
public static bool IsZero(double d1)
{
return (-.000001 < d1 && d1 < .000001);
}
public static bool IsZero2(double d1)
{
return (d1 > -.000001 && d1 < .000001);
}
public static bool IsZero3(double d1)
{
return (d1 > -.000001 && .000001 > d1);
}
static public void Main()
{
double sum;
{
Stopwatch st = new Stopwatch();
st.Start();
Console.WriteLine("Ignore - make sure any initialization needed in this
class is done");
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero2(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
{
sum = 0;
double x = UInt32.MaxValue;
Stopwatch st = new Stopwatch();
st.Start();
for (uint j = 0; j < UInt32.MaxValue; j++)
{
x = x - 1.0;
if (IsZero3(x))
{
sum += 1;
}
}
st.Stop();
Console.WriteLine(st.ElapsedMilliseconds);
Console.WriteLine(sum);
}
}
}
}