|
Viorel. wrote: return (n == 0 || n == 1) ? n : -1;
?: operator is slow.
Maxwell Chen
|
|
|
|
|
Maxwell Chen wrote: ?: operator is slow.
I do not think the ternary operator ?: is slower then an if statement, because in both cases the program is translated to the same series of microprocessor instructions, like CMP , JE , JMP . In my opinion, the ?: operator is a compact way for particular if , and makes the text shorter and probably clearer, but does not affect performance or code size.
|
|
|
|
|
My method is this way:
Build in Release mode, turn off the optimization.
int TestI(int n)
{
if(n==0 || n==1)
return n;
return -1;
}
int TestO(int n)
{
return (n == 0 || n == 1) ? n : -1;
}
void CTestDlg::OnBnClickedButton1()
{
DWORD s = GetTickCount();
for(UINT i = 0; i < 0xE00000; i++)
{
TestI(3);
}
DWORD e = GetTickCount();
DWORD d = e - s;
CString t;
t.Format("Time 0x%X", d);
AfxMessageBox(t);
}
void CTestDlg::OnBnClickedButton2()
{
DWORD s = GetTickCount();
for(UINT i = 0; i < 0xE00000; i++)
{
TestO(3);
}
DWORD e = GetTickCount();
DWORD d = e - s;
CString t;
t.Format("Time 0x%X", d);
AfxMessageBox(t);
} Result:
If-Else:
0xDC, 0xDC, 0xEC, 0xF0, 0xDC
? : operator:
0xFE, 0xF0, 0xFA, 0xF1, 0xFE
Maxwell Chen
|
|
|
|
|
I think it is not a problem if the compiler generates different code for looking-similar fragments when the optimization is turned off.
Since we are interested in faster code, we should turn the optimization on. But in this case we should take care that the compiler will not completely remove the fragments we investigate in our experiments. In order to prevent removing or in-lining of tested function and calls, we have to prepare our code using directives like volatile and __declspec(noinline) .
The experimental code for comparing code with optimizations can look like this:
int __declspec(noinline) TestI(int n)
{
volatile static int k = n;
if(n==0 || n==1)
return n;
return -1;
}
int __declspec(noinline) TestO(int n)
{
volatile static int k = n;
return (n == 0 || n == 1) ? n : -1;
}
void main()
{
const long N = 1000000000L;
DWORD s = ::GetTickCount();
for(long i = 0; i < N; ++i)
{
TestI(3);
}
DWORD t = ::GetTickCount() - s;
printf("%i\n", t);
s = ::GetTickCount();
for(long i = 0; i < N; ++i)
{
TestO(3);
}
t = ::GetTickCount() - s;
printf("%i\n", t);
}
In this case, the average time in case of VC++ 2003 compiler does not differ.
It should be more ways for performing accurate code-generation comparison in case the optimization is turned on.
In addition, this can be better checked by specifying a compiler option for generating intermediate Assembler code.
|
|
|
|
|
With your method,
NDEBUG, /MT, /Ot, /O2,
there is no difference between "if-else" and "?: operator" in the listing ASM code.
And regarding to the performance, ?: operator is a little faster.
Result:
9754 (if-else) (ignored, warm-up exercise)
9323 (? : ) (ignored, warm-up exercise)
9223 (if-else)
9214 (? : )
ASM code:
?TestI@@YAHH@Z PROC ; TestI, COMDAT
; 5 : volatile static int k = n;
mov eax, 1
test BYTE PTR ?$S1@?1??TestI@@YAHH@Z@4IA, al
jne SHORT $LN8@TestI
or DWORD PTR ?$S1@?1??TestI@@YAHH@Z@4IA, eax
mov DWORD PTR ?k@?1??TestI@@YAHH@Z@4HC, 3
$LN8@TestI:
; 6 : if(n==0 || n==1)
; 7 : return n;
; 8 : return -1;
or eax, -1
; 9 : }
ret 0
?TestI@@YAHH@Z ENDP ; TestI
?TestO@@YAHH@Z PROC ; TestO, COMDAT
; 13 : volatile static int k = n;
mov eax, 1
test BYTE PTR ?$S2@?1??TestO@@YAHH@Z@4IA, al
jne SHORT $LN8@TestO
or DWORD PTR ?$S2@?1??TestO@@YAHH@Z@4IA, eax
mov DWORD PTR ?k@?1??TestO@@YAHH@Z@4HC, 3
$LN8@TestO:
; 14 : return (n == 0 || n == 1) ? n : -1;
or eax, -1
; 15 : }
ret 0
?TestO@@YAHH@Z ENDP ; TestO
Maxwell Chen
|
|
|
|
|
Therefore, it seems that this compiler generates good enough code for both cases. We should be curious which behavior have other compilers in case of "if vs. ?:" problem.
|
|
|
|
|
Viorel. wrote: it seems that this compiler generates good enough code for both cases.
BTW, I am using VC++ 2005.
Maxwell Chen
|
|
|
|
|
or this
<br />
int Test(int n)<br />
{<br />
int nReturn = 0 ;<br />
switch(n)<br />
{<br />
case 0:<br />
case 1:<br />
nReturn=1;<br />
break;<br />
default:<br />
nReturn=-1;<br />
break;<br />
}<br />
return nReturn;<br />
}<br />
whitesky
|
|
|
|
|
PLZ HELPME SEND FULL CODE
how to fetch, store ,delete and update date in mysql table from MFC at run time from vc++ code(MFC)
PLZ SEND ME THIS CODE TO HELP THANKS FOR THIS
gourav dogra jfjunf
|
|
|
|
|
|
Hi,
I have lauchned an application B from my application A using ShellExecute() , it will return a Handle.
How can I track in application A that application B is closed at certain stage?
Thanks
Regards.
|
|
|
|
|
First of all what type of application you are creating.
Here you can chect the value of handle....
check if it exist...
Cheers
"Peace of mind through Technology"
|
|
|
|
|
Does it means that If when 2nd application is closed then the handle returned by ShellExecute will automaticlay become flase or expired or null.
Regards.
|
|
|
|
|
|
I don't think there is an easy way to track an application. The Handle returned by ShellExecute() is not a handle, but a value.
There are other ways you can track an application like: Events , Mutexes etc.
this is this.
|
|
|
|
|
zahid_ash wrote: Hi,
I have lauchned an application B from my application A using ShellExecute() , it will return a Handle.
How can I track in application A that application B is closed at certain stage?
You may keep track of process by enumrating the running processes in specific time stamp.
Knock out 't' from can't,
You can if you think you can
|
|
|
|
|
if u r application happens to be an .exe then u can get Processid.Using it u can track application B in Application A.This might help u.
never say die
|
|
|
|
|
Process32First and Process32Next to get the Processid:->
never say die
|
|
|
|
|
In order to see that a process was closed, you need its "handle". Please compare ShellExecuteEx and CreateProcess in order to see which is more suitable to obtain the handle. Then use the handle in the following mode:
if(::WaitForSingleObject(handle, 0) == WAIT_OBJECT_0)
{
}
else
{
}
If you want to wait until the process is closed, then specify a timeout value in WaitForSingleObject . See this function for more details.
|
|
|
|
|
Use SHELLEXECUTEINFO with ShellExecuteEx() and the structure has a member "hProcess" this is the handle to newly created process.
suhredayan There is no place like 127.0.0.1
|
|
|
|
|
HI.
I am running my application using command prompt.
like C:\> D:\App.exe
after launching this Application the command prompt come back to prompt again
like C:\>
does it means that application has been ended . but the appliccation is still running
then how can I come to know that application is still running using command prompt
thanks
Regards.
|
|
|
|
|
when you type : c:\ D:\App.exe and comes back to c:\
it will happen ...Here you are not changing the drive to access but just specifying the path of your exe.
Your exe will start as a new process ....so you have to eplicitly terminate your application
Cheers
"Peace of mind through Technology"
|
|
|
|
|
OK, Thanks.
But I have one more question that when we run Format command or some other command on Comand prompt, then why these comand are not run as new process. they usualy run in same command prompt and will not free the prompt until the command execution ends
Regards.
|
|
|
|
|
zahid_ash wrote: Format command or some other command on Comand prompt
That is because those are Command-line applications. Back in the days of DOS, all applications were Command-line.
this is this.
|
|
|
|
|
zahid_ash wrote: HI.
I am running my application using command prompt.
like C:\> D:\App.exe
after launching this Application the command prompt come back to prompt again
like C:\>
does it means that application has been ended . but the appliccation is still running
then how can I come to know that application is still running using command prompt
if the application is Command prompt based then it runs fine...
but if it is windows based then application chooses the windows platform to run and application's info enters into windows enviornment not into DOS environment even it run from command prompt.
if you want to check the application running state then you have to check into running processes by enumarating processes or any other way.
Knock out 't' from can't,
You can if you think you can
|
|
|
|