Why the performance difference with the following code loops.

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);
}



}
}
}
 
F

Family Tree Mike

Dave Harris said:
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);
}



}
}
}


The first loop is fastest in part, because the first test of the two is
always false, therefore the second test is not done.
 
F

Family Tree Mike

Family Tree Mike said:
The first loop is fastest in part, because the first test of the two is
always false, therefore the second test is not done.


Sorry, ignore that. I read it wrong... Switch case one's logic arround
though and I think it should be faster.
 

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