Click here to Skip to main content
15,886,693 members
Please Sign up or sign in to vote.
3.00/5 (3 votes)
See more:
I have a question about run speed. Is this:

C#
if( (x1 == x2) && (x2 == x3) && (x3 == x4) && (x4 == x5) ) return true;
else
    return false;


faster, slower or about the same as this:

C#
if( x == x2)
{
    if( x2 == x3)
        if(x3 == x4)
            if(x4 == x5)
                return true;
}
else
    return false;
Posted

Be sure to repeat the timing experiment several times because the measurements will fluctuate.

In compiled code, there is no reason the speeds should be different, they will translate to exactly the same assembly code.

You could also write

return x1 == x2 && x2 == x3 && x3 == x4 && x4 == x5;

same speed.

And possibly slightly different speed but same effect:

C++
return x1 == x2 && x1 == x3 && x1 == x4 && x1 == x5;


If the x's are integers, it is certainly worth to try:

C++
return ((x1 ^ x2) | (x2 ^ x3) | (x3 ^ x4) | (x4 ^ x5)) == 0;
 
Share this answer
 
v2
Comments
NathanielM 29-Jul-11 14:53pm    
it's actually an array of booleans representing a string of bits. I'm attempting to go through a long string and see if there are 5 bits in a row that are all high (true) or low (false).
I'm using the if > if > if > if version because then if I find 3 in a row, but the 4th is different, I can skip the 3 bits in the else clause instead of incrementing by one and seeing that the next two bits in a row are the same, etc.
YvesDaoust 29-Jul-11 15:52pm    
I see. This opens up a number of new possibilities.

On one hand, you can use one of the classical string searching algorithms. Some of them are linear in the string length, regardless the pattern length. See http://en.wikipedia.org/wiki/String_searching_algorithm.

On another hand, you can pack your bits into 32 or 64 integers and use bitwise operations. X & (X<<1) & (X<<2) & (X<<3) & (X<<4) finds all 5 contiguous 1s in a 28/60 bits sequence.

Using SSE instructions, you can even process 128 bits at a time...
YvesDaoust 29-Jul-11 16:03pm    
Forget about the string searching stuff, overkill here. Just count the number of equal bits in a row with a two-state automaton.

bool State= Bit[0];
int Count= 1;
for (i= 1; i < N; i++)
{
if (State == Bit[i])
{
Count++;
if (Count == 5)
break;
}
else
{
Count= 1;
State= Bit[i];
}
}
NathanielM 29-Jul-11 16:25pm    
Again, sometimes some of the simplest answers are the hardest to think of. Thanks!
YvesDaoust 29-Jul-11 16:16pm    
You can spare a test by using a 10 states automaton. The 10 states represent the number of contiguous 0s or 1s that have been seen so far. You can represent the transition matrix as an array.
C#
/// <summary>
/// Performance Counter to calculate function executing time.
/// </summary>
public class Performance
{
    public delegate void CalculateHanlder();
    /// <summary>
    /// Using Performance.CalculFunction(MethodName)
    /// It will print executing time in Console.
    /// </summary>
    /// <param name="method">A none argument method</param>
    public static void CalculateMethod(CalculateHanlder method)
    {
        Stopwatch st = new Stopwatch();
        st.Start();
        method.Invoke();
        st.Stop();
        string msg = string.Format("Elapsed = {0} in [{1}]", st.Elapsed.ToString(), method.Method.ToString());
        Console.WriteLine(msg);
        Debug.WriteLine(msg);
    }
}
bool Test(int x1, int x2, int x3, int x4, int x5)
{
if( (x1 == x2) && (x2 == x3) && (x3 == x4) && (x4 == x5) ) return true;
else
    return false;
}
bool Test2(int x1, int x2, int x3, int x4, int x5)
{
if( x == x2)
{
    if( x2 == x3)
        if(x3 == x4)
            if(x4 == x5)
                return true;
}
else
    return false;
}
void TestIf()
{
    for(int i = 0 ; i < 100000; i ++)
        Test(1,2,3,4,5);
}
Performance.CalculateMethod(TestIf);

You can use these code to test.
Using StopWatch can count the executing time.
This is my library function, you can pass a none parameter function to tick.
 
Share this answer
 
Comments
NathanielM 29-Jul-11 12:37pm    
test = 00.0016340 seconds
test2 = 00.0016998 seconds

Thanks, for some reason, sometimes the simplest solution is the hardest one to think of.
Tarun.K.S 29-Jul-11 13:28pm    
Excellent answer. 5+
Uday P.Singh 29-Jul-11 13:32pm    
good answer my 5! too.
YvesDaoust 29-Jul-11 13:33pm    
For a complete investigation, you need to try other sets of values, those with equal arguments. (The function should run about four times slower with all equal arguments.)
Espen Harlinn 30-Jul-11 7:34am    
Good work, my 5
Try it and see. You could learn something
 
Share this answer
 
Better use Timer yourself in the code to see the result.

Recently a survey(which contains many discussions) in CP similar to your question, see that

Do you wrap single line statements in braces (or begin/end)?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900