Click here to Skip to main content
15,891,204 members
Please Sign up or sign in to vote.
1.44/5 (3 votes)
See more:
Hi Everybody,

There are two conditions which can provide me the output. But the issue is this which one is better and perform faster. Why? They both condition will provide the same output.

a b c d are string variables.

Case 1 :

if (a==c && b==d) {
   // Output
}

Case 2 :

if (a+ b == c+d) 
{
//Output
}
Posted
Updated 29-Oct-14 3:30am
v2
Comments
V. 29-Oct-14 9:17am    
I would guess the first one will be faster, but you'll only know for sure when timing the two...
Anubhava Dimri 29-Oct-14 9:21am    
Need reason....
ZurdoDev 29-Oct-14 10:15am    
Then you run the tests and figure it out. Don't expect others to do your homework.
King Fisher 29-Oct-14 9:19am    
Interview Question?
Anubhava Dimri 29-Oct-14 9:20am    
no matter....

Whether a,b,c,d are integers (first version OP), or are strings (second version OP), comparing their possible speed of execution must take into account the distribution in the tests of values of 'a and 'b such that the 'a is not equal to 'c.

Because: the logical And operator && in C# will not execute the second condition (b == d) when (a != c); this is "short-circuit" optimization.

So, to the extent there are a large number of tests where a != c, the first test should be much faster than the second, which will always add 'a and 'b.

We'd also expect the first test to be faster in general because the addition of two strings creates a new string object, and that's done twice in the second test.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Oct-14 18:24pm    
Good point, a 5.
See also my comments to the Solution 1, which has a fundamental flaw.
—SA
Anubhava Dimri 30-Oct-14 4:07am    
Reasonable .
No (as correctly noted by BillWoodruff and PIEBALDconsult) they don't provide the same output. You should focus your energy on understanding the difference of such pieces of code instead of worrying about performance.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Oct-14 18:17pm    
5ed!
—SA
CPallini 30-Oct-14 7:00am    
Thank you, Sergey.
C#
int a = 5;
int b = 7;
int c = 19;
int d = 7;

System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

sw.Start();
for(int i = 0; i < 1000000; i++){
    if (a==c && b==d);
}
sw.Stop();

Console.WriteLine("(a==c && b==d) took " + sw.ElapsedMilliseconds + " milliseconds");

sw.Start();
for(int i = 0; i < 1000000; i++){
    if (a+b == c+d);
}
sw.Stop();
Console.WriteLine("(a+b == c+d) took " + sw.ElapsedMilliseconds + " milliseconds");
Console.ReadLine();

output:
(a==c && b==d) took 3 milliseconds
(a+b == c+d) took 7 milliseconds.


Since you so impolitely stated into the reply you need a reason (which you didn't state in the question) I will let you figure it out yourself.

[EDIT]You did state it, but still you could remain more polite[/EDIT]
 
Share this answer
 
v2
Comments
KaushalJB 29-Oct-14 9:31am    
Nice one
V. 29-Oct-14 9:34am    
thanks :-)
Anubhava Dimri 29-Oct-14 9:37am    
Thanks Great... Acceptable
BillWoodruff 29-Oct-14 9:54am    
In "real-world" conditions it is unlikely that a,b,c,d are constants, which is what you are comparing here. Any test that attempts to be meaningful in terms of actual use in an application must vary the values of a,b,c,d.
V. 29-Oct-14 9:56am    
I thought of that, but I figured in this example that would be overkill ;-)

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