PDA

Click to See Complete Forum and Search --> : A question about If-else and (condition)?: operator


debbie_82
Feb 2nd, 2004, 09:52 PM
What do I save when I substitute the following code:


if ( pItemData->nSelCheck )
iImg = 1;
else
iImg = 0;


with this one:


iImg = ( pItemData->nSelCheck )? 1: 0;

crptcblade
Feb 2nd, 2004, 10:47 PM
Three lines

debbie_82
Feb 2nd, 2004, 10:56 PM
Originally posted by crptcblade
Three lines

Funny.

Yes definitely... :D
I know that but what I mean is in terms of processing, whatever it's called...
Will the program run faster or slower.
How does the CPU process the latter statement?

packetVB
Feb 4th, 2004, 09:47 AM
I seen the post and was really curious about this and tested it out.

Seems MSVC generates one less assembly instruction for the first,if branch.
Whether that translates into faster i guess would depend on the CPU.

my worthless two cents.

:)

si_the_geek
Feb 4th, 2004, 10:32 AM
with the VB equivalent there is a functionality difference, I don't know if it works the same way.

In vb it would be:
If pItemData -> nSelCheck Then
iImg = 1
Else
iImg = 0
End If

'or:
iImg = Iif( pItemData->nSelCheck, 1, 0)

(nb: I dont know what the -> operator should be in vb! :blush: )

the second one here also takes slightly longer to execute, but it can easily be made more significant for one important reason: In the first method only the required result is calculated, in the second both possible results are calculated, which will take much longer if there is a complex calculation or a function call in either result.

CornedBee
Feb 4th, 2004, 10:47 AM
Depending on the compiler and such simple branching, there is usually no difference at all. If the second version might be faster (by using special CPU instructions like the x86's conditional set), a good compiler should realize that the same is applicable to the first code.

So, it really depends on the compiler. The real advantage of ?: lies in the brevitiy and (sometimes) readability.

debbie_82
Feb 4th, 2004, 07:47 PM
Originally posted by packetVB
I seen the post and was really curious about this and tested it out.

Seems MSVC generates one less assembly instruction for the first,if branch.
Whether that translates into faster i guess would depend on the CPU.

my worthless two cents.

:)


How do I know how many assembly instruction a statement can generate in MSVC? I'd like to know how, I'm a newbie at MFC so I have yet to learn a lot of its functionalities... :lol:

debbie_82
Feb 4th, 2004, 07:51 PM
Originally posted by si_the_geek
with the VB equivalent there is a functionality difference, I don't know if it works the same way.

In vb it would be:
If pItemData -> nSelCheck Then
iImg = 1
Else
iImg = 0
End If

'or:
iImg = Iif( pItemData->nSelCheck, 1, 0)

(nb: I dont know what the -> operator should be in vb! :blush: )

the second one here also takes slightly longer to execute, but it can easily be made more significant for one important reason: In the first method only the required result is calculated, in the second both possible results are calculated, which will take much longer if there is a complex calculation or a function call in either result.

So when is it advisable to use Iif? I did not know about this though and also, what is the great thing about IIF, that MS introcuced this statement?

CornedBee
Feb 5th, 2004, 03:25 AM
Originally posted by debbie_82
How do I know how many assembly instruction a statement can generate in MSVC? I'd like to know how, I'm a newbie at MFC so I have yet to learn a lot of its functionalities... :lol:

You can tell the compiler to write the assembly it generates into a file. This is only interesting in a release build, because the generated assembly differs greatly between a debug and release build.

And don't confuse MFC with MSVC or C++. MFC is a class library which I think you should avoid, at least until you've properly learned C++.

si_the_geek
Feb 5th, 2004, 04:55 AM
Originally posted by debbie_82
So when is it advisable to use Iif? I did not know about this though and also, what is the great thing about IIF, that MS introcuced this statement?
it's just an inline-if, most languages have it in some form. It allows you to write shorter & more readable code.

it just happens that in VB it is a function, and VB calculates all parameters for a function before the function call.

there's no problem if both parameters are fixed values (which is when you should use it).

it's only when you start complex calculations that there becomes an issue. if you call functions within the parameters then those functions will always be called (which may or may not be what you want!).

debbie_82
Feb 9th, 2004, 01:55 AM
Originally posted by CornedBee
You can tell the compiler to write the assembly it generates into a file. This is only interesting in a release build, because the generated assembly differs greatly between a debug and release build.

And don't confuse MFC with MSVC or C++. MFC is a class library which I think you should avoid, at least until you've properly learned C++.

Sure boss... sorry about the mix-up...:D ...


Is it in the Settings?
Whatever... Anyway... I don't know how to set it cause there are so many options and I don't understand the options cause my ide is japanese version and I don't understand japanese... :D ...(still studying it though)... I have no choice cause the programs won't run if it is not japanese...

One question... does it really matter if one program is written in a japanese version of MSVC and subsequently you can't run that program in an english version of MSVC and vice versa?

What we tried that didn't work is if the program is written in japanese version of MSVC and the locale settings of the computer is english... The program does not generate the .pcb file or something...weird errors

vbNeo
Feb 9th, 2004, 09:57 AM
Originally posted by si_the_geek
it's just an inline-if, most languages have it in some form. It allows you to write shorter & more readable code.

it just happens that in VB it is a function, and VB calculates all parameters for a function before the function call.

there's no problem if both parameters are fixed values (which is when you should use it).

it's only when you start complex calculations that there becomes an issue. if you call functions within the parameters then those functions will always be called (which may or may not be what you want!).

Can you tell VB to write an ASM file as well ?

si_the_geek
Feb 9th, 2004, 10:50 AM
:confused:

it's not that kind of inline Neo (although I think I heard you can use ASM somehow), it just means just in-the-same-line as the code, so for example you can build a string with multiple possibilities, and not write lots of lines of If's. dodgy example:

fred = 0
joe = 0
'...
my_string = "fred is " & iif(fred <> joe, "NOT ","") & iif(joe = 0," zero", joe) & "."

which without the inline-if would be:

fred = 0
joe = 0
'...
dim JoeString as string
If joe = 0 Then
JoeString = "zero"
Else
JoeString = CStr(Joe)
End If
If fred = joe then
my_string = "fred is " & JoeString & "."
Else
my_string = "fred is NOT " & JoeString & "."
End If



Basically IIF is this function:

Public Function IIF (condition as Boolean, TruePart, FalsePart)
If condition Then
IIF = TruePart
Else
IIF = FalsePart
End If
End Function

CornedBee
Feb 9th, 2004, 05:43 PM
I don't know that much about VB. I can say for sure that you can't tell VB5 to show the ASM, because it never generates any. I don't know if VB6 generates assembly. I'm pretty sure you can tell the VB.Net compiler to write the MSIL assembly to a file.