Which among following statements c/c++ code is faster?
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
It depends on the compiler and the optimization options. But in this simple case the generated code should be identical. You can check this by specifying a compiler option to generate assembly code (e.g. /Fas with Microsoft compilers) and compare the generated code.
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
You don't need to care; write readable code, the compiler will be more than sufficient to optimize it. They will be compile to the same thing.
I'd rather be phishing!
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
-
You don't need to care; write readable code, the compiler will be more than sufficient to optimize it. They will be compile to the same thing.
I'd rather be phishing!
I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..
-
I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..
THERE IS NO DIFFERENCE. For such simple code, the compiler will generate the same code. compile and generate the assembly code for both versions, and you will see that the code is the same. (testes with debug version on VS2012).
I'd rather be phishing!
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
With something this trivial, it doesn't matter. If you want to write fast code, there are a ton of other bottlenecks that are more important than this.
-
I think the first one is faster. because, the in second - variable first ! converted - the newly converted variable is converted to 1/0 for if statement but in first- -the == operator check bit wise datas of 2 numbers, if matched 1/if not 0. so i think the first one is faster. Whats your view..
-
int i = 0; statement 1: if( i == 0 ) statement 2: if( !i ) Although the speed does not matters in this case, but I want to know the way above 2 statements are evaluated.
Statement 2 should be faster. - no need to do parsing and comparison, just go to memory to negate, and see if it's above 0 or not. - compiler can generate machine code that would take less clock circles to execute the if() statement The speed differences between the 2 statements are negligible, - but statement 1 reads better, easier to understand and for better documentation.
-
Statement 2 should be faster. - no need to do parsing and comparison, just go to memory to negate, and see if it's above 0 or not. - compiler can generate machine code that would take less clock circles to execute the if() statement The speed differences between the 2 statements are negligible, - but statement 1 reads better, easier to understand and for better documentation.
-
It depends on the compiler and the optimization options. But in this simple case the generated code should be identical. You can check this by specifying a compiler option to generate assembly code (e.g. /Fas with Microsoft compilers) and compare the generated code.
I agree with you! In general, two cases are identical!