|
Hi all,
This might be a simple question, and you might say that both ways have their own pros and cons (and the pros and cons are so minor, that they are negligible, therefore they are virtually equally efficient). But sometimes I wonder if every single bit of memory and every cpu cycle count, which one is better?
Thanks for any input.
<br />
for(int i=0; i<myArray.GetSize(); i++)<br />
{<br />
if(myArray.GetAt(i).GetValue()==1) { Function1(); }<br />
else if(myArray.GetAt(i).GetValue()==2) { Function2(); } }<br />
else { Function3(); }<br />
}<br />
<br />
for(int i=0; i<myArray.GetSize(); i++)<br />
{<br />
int nValue = myArray.GetAt(i).GetValue();<br />
if(nValue==1) { Function1(); }<br />
else if(nValue==2) { Function2(); }<br />
else { Function3(); }<br />
}<br />
|
|
|
|
|
The answer is, it depends .
If the value returned by <nobr>myArray.GetAt(i).GetValue() is 1, then the two methods will probably have equivalent performance. The fact that you've got a named temporary variable in method 2 doesn't affect the performance, since the compiler generates a temporary variable to hold the return value in method 1.
If the value returned by <nobr>myArray.GetAt(i).GetValue() is >= 2, method 2 will probably execute faster. In method 1, the compiler will generate two calls to <nobr>myArray.GetAt(i).GetValue() . Method 2 only uses a single call. The fact that the function call probably returns the same value is irrelevant, since the compiler can not make that assumption, and therefore must generate and execute code for two calls.
If <nobr>myArray.GetAt(i).GetValue() does not have side effects, then it's safe to assign its value to a temporary and use that, as in method 2.
If <nobr>myArray.GetAt(i).GetValue() does have side effects (successive calls with the same arguments do not return the same results), then you must use method 1.
Software Zen: delete this;
|
|
|
|
|
...but i would like to add
for(int i=0; i<myArray.GetSize(); i++)
myArray.GetSize() is called in every loop, so there is another method call in every loop. you can avoid that by using a temp variable there if the count/loop is large.
I'll write a suicide note on a hundred dollar bill - Dire Straits
|
|
|
|
|
Joe Smith IX wrote:
for(int i=0; i<myArray.GetSize(); i++)
{
int nValue = myArray.GetAt(i).GetValue();
if(nValue==1) { Function1(); }
else if(nValue==2) { Function2(); }
else { Function3(); }
}
How about,
int size = myArray.GetSize ();
for ( int i = 0; i < size; ++i )
{
switch ( myArray.GetAt ( i ).GetValue ())
{
case 1 :
Function1 () ;
break ;
case 2 :
Function2 () ;
break ;
default :
Function3 () ;
break ;
}
}
But if efficiency is relevant the whole structure needs examination.
Paul
|
|
|
|
|
Method (1) is dangerous - it requires that GetValue() has no side-effects. This makes for code that's hard to maintain.
/ravi
My new year's resolution: 2048 x 1536
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
First, the line else if(myArray.GetAt(i).GetValue()==2) { Function2(); } } has an extra brace at the end.
In actual performance the two are identical since they compile to the same thing (at least in VC++ 7.1, but likely also in VC++ 6.0.)
Incidentally, using a switch statement is slightly slower. If there were additional cases, that could change.
[EDIT: At Tim correctly points out, my answer is correct only to the narrow stipulation given. Since GetValue() could involve more than the return of a simple value, it is best to get the value first then test it. The exception would be IF side effects are documented and each test depends on those side effects, though that would be a very odd solution indeed.]
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
What if the definition of GetValue is in another CPP module? Depending on the link time optimization, that code might not be hoisted into the main routine. Thus the multiple GetValue version would not result in the same code.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
That wasn't part of his stipulated conditions.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Nor was the implementation of the routine being inside the same cpp or made inline.
The only reason I bring this up is because you don't tell the full story. What you said is totally true but only under certain conditions.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
In general, you should use the second version.
When you have full knowledge of how GetValue is implemented, you can decide which is better. However, that makes caller dependent on the implementation of GetValue. If you ever need to change the implementation, any performance decisions might come back to haunt you. You really shouldn't be depending on GetValue being implemented as it is. Thus that makes the second version the better option.
You should always try to keep relationships between two classes limited to interface only and never the actual implementation. You can't always do this, but when you can, it is best that you do.
Tim Smith
I'm going to patent thought. I have yet to see any prior art.
|
|
|
|
|
Is it free to use the MFC source code however you want, or is that off limits. I don't want to use MFC, but I would like to use bits and pieces of it, like CRect etc. Is this legal, or is it violating any copyrights?
|
|
|
|
|
It's completely legal on the condition that you don't use it to write, and I presume sell, a class library or otherwise represent the code as your own.
Microsoft also requests that you don't compile the new results with the same name as the current MFC libraries.
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Hi All,
I am trying to send data from my client to my server through an RPC call in Unix C. I am getting following error:
ServerNameHere: RPC: Unable to send; errno = Bad file number; Illegal file descriptor
I am unaware of nitty gritties of RPC, can anyone help?
I need urgent help. Thanks in advance for any help provided.
Just to elloborate on problem, this error normally comes when I try sending some data 2nd time. I am sending different data each time(taken by user input) in a loop.
Regards,
Ahsan
|
|
|
|
|
Knock Knock...
Anybody home? Someone please answer.
|
|
|
|
|
This is a Visual C++ forum on a site with an almost exclusive Microsoft bias. You're not likely to get help with a UNIX RPC problem here, I'm afraid.
Software Zen: delete this;
|
|
|
|
|
I am a newb to programming, so I use the MFC wizard in VC++ 2003. I would like to have access to the WindowProc function (the function that recieves all the messages), but it is locked. Does anyone know how I could edit the WindowProc function? Anyone that could help, that would be great.
|
|
|
|
|
I need stop the program,
<br />
<br />
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {<br />
if (stricmp(lpCmdLine, "-noautorun") == 0) {<br />
autorun(); <br />
i cant juse break; <br />
<br />
}else if (lpCmdLine == NULL || _tcslen(lpCmdLine) == 0) {<br />
<br />
MessageBox(NULL, _T("No switch specified"), g_pszCaption, MB_OK | MB_ICONEXCLAMATION);<br />
return 0;<br />
}<br />
<br />
void autorun(){<br />
if void autorun() ends, <br />
the program needs to stop running <br />
<br />
i cant juse return 0;<br />
<br />
return 0;<br />
}<br />
error C2562: 'autorun' : 'void' function returning a value<br />
<br />
thanx for helping me
Greets Jeroen
|
|
|
|
|
Obviously voids can't return a value but I think you could use ExitProcess API or maybe TerminateProcess API. Try this link
-Ryan M.
|
|
|
|
|
Thanx ryan M.
<br />
void autorun () {<br />
<br />
ExitProcess(0); <br />
}}<br />
Jeroen .b
|
|
|
|
|
don't exit() function work too ?
TOXCCT >>> GEII power
|
|
|
|
|
Sorry, I don't know if this autorun() -function is something special, but if you have a function which has void as return type you can just use the instruction return; to end the function (without any value) or just leave out the return instruction completely.
Hope it helps...
Regards, mYkel
|
|
|
|
|
no i cant use return;
or leave out the return instruction
it gives errors with other functions
ExitProcess(0); works good
Greets jeroen
thanx for your time
|
|
|
|
|
|
he CANNOT return a value if the function is told as "not returning a value", which is the meaning of void here !!!
TOXCCT >>> GEII power
|
|
|
|
|
<br />
void autorun (void) {<br />
<br />
}<br />
exit (0);<br />
}<br />
Jeroen.b
|
|
|
|