What do I save when I substitute the following code:
with this one:Code:if ( pItemData->nSelCheck )
iImg = 1;
else
iImg = 0;
Code:iImg = ( pItemData->nSelCheck )? 1: 0;
Printable View
What do I save when I substitute the following code:
with this one:Code:if ( pItemData->nSelCheck )
iImg = 1;
else
iImg = 0;
Code:iImg = ( pItemData->nSelCheck )? 1: 0;
Three lines
Funny.Quote:
Originally posted by crptcblade
Three lines
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?
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.
:)
with the VB equivalent there is a functionality difference, I don't know if it works the same way.
In vb it would be:(nb: I dont know what the -> operator should be in vb! :blush: )VB Code:
If pItemData -> nSelCheck Then iImg = 1 Else iImg = 0 End If 'or: iImg = Iif( pItemData->nSelCheck, 1, 0)
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.
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.
Quote:
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:
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?Quote:
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:(nb: I dont know what the -> operator should be in vb! :blush: )VB Code:
If pItemData -> nSelCheck Then iImg = 1 Else iImg = 0 End If 'or: iImg = Iif( pItemData->nSelCheck, 1, 0)
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.
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.Quote:
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:
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++.
it's just an inline-if, most languages have it in some form. It allows you to write shorter & more readable code.Quote:
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 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!).
Sure boss... sorry about the mix-up...:D ...Quote:
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++.
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
Can you tell VB to write an ASM file as well ?Quote:
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!).
: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:
which without the inline-if would be:VB Code:
fred = 0 joe = 0 '... my_string = "fred is " & iif(fred <> joe, "NOT ","") & iif(joe = 0," zero", joe) & "."
VB Code:
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:
VB Code:
Public Function IIF (condition as Boolean, TruePart, FalsePart) If condition Then IIF = TruePart Else IIF = FalsePart End If End Function
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.