Original Post
execution wise, are they the same, or is one faster than the other?
int main(){ int i = rand() % 5; switch(i) { case 0: i = 0; break; case 1: i = 1; break; case 2: i = 2; break; case 3: i = 3; break; case 4: i = 4; break; case 5: i = 5; break; } std::cout << i;}int main(){ int i = rand() % 5; if(i == 0) { i = 0; } else if(i == 1) { i = 1; } else if(i == 2) { i = 2; } else if(i == 3) { i = 3; } else if(i == 4) { i = 4; } else if(i == 5) { i = 5; } std::cout << i;} call dword ptr [__imp__rand (4020A8h)] cdq mov ecx,5 idiv eax,ecx cmp edx,ecx ja $LN1+2 (401081h) jmp dword ptr (401094h)[edx*4] $LN6: mov ecx,dword ptr [__imp_std::cout (40203Ch)] xor edx,edx push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret $LN5: mov ecx,dword ptr [__imp_std::cout (40203Ch)] mov edx,1 push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret $LN4: mov ecx,dword ptr [__imp_std::cout (40203Ch)] mov edx,2 push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret $LN3: mov ecx,dword ptr [__imp_std::cout (40203Ch)] mov edx,3 push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret $LN2: mov ecx,dword ptr [__imp_std::cout (40203Ch)] mov edx,4 push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret $LN1: mov edx,ecx mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret lea ecx,[ecx] call dword ptr [__imp__rand (4020A8h)] cdq push 5 pop ecx idiv eax,ecx mov eax,edx sub eax,0 je main+37h (401037h) dec eax je main+32h (401032h) dec eax je main+2Dh (40102Dh) dec eax je main+29h (401029h) dec eax je main+25h (401025h) dec eax jne main+39h (401039h) push ecx jmp main+2Fh (40102Fh) push 4 jmp main+2Fh (40102Fh) push 3 jmp main+2Fh (40102Fh) push 2 pop edx jmp main+39h (401039h) xor edx,edx inc edx jmp main+39h (401039h) xor edx,edx mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret call dword ptr [__imp__rand (4020A8h)] cdq mov ecx,5 idiv eax,ecx test edx,edx jne main+22h (401022h) mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret cmp edx,1 jne main+37h (401037h) mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret cmp edx,2 jne main+4Ch (40104Ch) mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret cmp edx,3 jne main+61h (401061h) mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret cmp edx,4 jne main+76h (401076h) mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret cmp edx,ecx jne main+7Ch (40107Ch) mov edx,ecx mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret call dword ptr [__imp__rand (4020A8h)] push 5 cdq pop ecx idiv eax,ecx test edx,edx je main+34h (401034h) cmp edx,1 je main+34h (401034h) cmp edx,2 jne main+1Dh (40101Dh) push edx jmp main+33h (401033h) cmp edx,3 jne main+25h (401025h) push edx jmp main+33h (401033h) cmp edx,4 jne main+2Dh (40102Dh) push edx jmp main+33h (401033h) cmp edx,5 jne main+34h (401034h) push edx pop edx mov ecx,dword ptr [__imp_std::cout (40203Ch)] push edx call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (402038h)] xor eax,eax ret Quote:Is that in a release build? I would have thought the compiler would optimise out the entire if / switch statement in this case...
Original post by Mxz
If you take Visual Studio 2005 as an example, and the following two code samples as a simple test.
Switch statement version
*** Source Snippet Removed ***
if-else statement version
*** Source Snippet Removed ***
Under the compiler optimization setting Favor fast code, the following disassembly is produced for the switch statement version.
*** Source Snippet Removed ***
As you can see, there are just a couple of jumps at the top of the function. However, the same code under the Favor small code optimization setting compiles down to the following.
*** Source Snippet Removed ***
On this compiler setting the select statement has been turned into a series of comparisons and jumps.
The if statements on the other hand produce the following output when the Favor fast code optimization is set.
*** Source Snippet Removed ***
Here we have many comparisons and jumps taking place.
The Favor small code setting produces the following out of the if statement.
*** Source Snippet Removed ***
As you can see, for Visual Studio the if statements in this case have been implemented using many jumps under both compiler settings. The select statement was implemented using a jump table when using the favor fast code optimization setting, but was implemented using a series of comparisons and jump statements under the favor small code setting.
In short it depends on your compiler, code and compiler settings. It is hard to make general statements about these things. The best way is to profile and see.
Quote:
Original post by PrettyBoyTim
I'm pretty sure you're looking for the optimisation in the wrong place.
From the sounds of it, your problem is to do with the rate that you're getting input messages into your application. When you move the mouse fast, there will be a larger difference between subsequent mouse positions that are sent to your program. Therefore if you move the mouse fast out of your program's window, the last mouse position message it gets will be a fair distance from the side of the window. If you wish to fix this you will need to intercept mouse move messages that happen outside your window as well.
Quote:The message pump shouldn't be a bottleneck at all. Considering your CPU can execute hundreds of millions of jumps per second, a switch() versus and if() will make no relevant difference.
Original post by pavel989 Quote:
Original post by PrettyBoyTim
I'm pretty sure you're looking for the optimisation in the wrong place.
From the sounds of it, your problem is to do with the rate that you're getting input messages into your application. When you move the mouse fast, there will be a larger difference between subsequent mouse positions that are sent to your program. Therefore if you move the mouse fast out of your program's window, the last mouse position message it gets will be a fair distance from the side of the window. If you wish to fix this you will need to intercept mouse move messages that happen outside your window as well.
well i just read a few hours ago, the msg thing, its a queue, so my program should be getting msgs down to the last one in the list, i just think its not sending data fast enof. who knows, ima look into the msg system and see what alternative or improvements there are.
Quote:That's because you don't get WM_MOUSEMOVE messages when the pointer is outside of the window. If you need that, use SetCapture. It doesn't matter how fast you process messages, you won't get one for every single pixel the mouse travels over.
Original post by pavel989
if the mouse flies out the window really fast, the points stop far behind the edge, so i thought the switch was slowing it down.
Quote:
Original post by pavel989
well i just read a few hours ago, the msg thing, its a queue, so my program should be getting msgs down to the last one in the list, i just think its not sending data fast enof. who knows, ima look into the msg system and see what alternative or improvements there are.
Quote:
Original post by Deyja
Additionally, erroneous branch predictions can cause the large jump table in the switch version to stall, while the if-elseif chain, with nothing but small jumps, would run faster.
Quote:
Original post by Daaark
I've always thought of the switch statement as simple syntax sugar for what would be an overcomplicated to type and read series of if statement blocks.
This topic has been locked by a moderator. New replies are not allowed.
With your permission, GameDev.net uses analytics cookies to understand how people use the platform. You can accept analytics or continue with necessary cookies only. Learn more