Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Jeff D

Switch statements

This topic is 5797 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I use switch a lot when I program, just wondering if I have a switch statement of like ~20 to 30 possible ways could this slow down the program? Jeff D

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I don''t think it will slow it down but I have read some books on coding style and many people argue that using alot of switch statements makes thinks hard to keep track of. The basic argument is what happens when you make a small change? You have to go all over the place making sure your switch statements get updated.

Share this post


Link to post
Share on other sites
It might, but doing 30-40 "if" statements aren''t going to be any more efficient, and if it''s a necessary step in the program there really isn''t any way around it I think. The only thing you might do is limit the amount of times that the program will run through that switch statment (like when it''s not necessary).

You might also try nested "if" statements, which can act like a logic tree. In some cases these can be more efficient because they will run through only one thread of greater section of code. However coding this can be a little trickier.

Share this post


Link to post
Share on other sites
Sometimes, when the case blocks are similar, it might be better t construct a table and search it instead. It will be smaller & faster and cleaner source code.

An example might be how to handle DirectX error codes. First I used something like this:

char * GetDDReturnString(HRESULT hr)
{
switch(hr) {
case DD_OK: return "DD_OK";
case DDERR_ALREADYINITIALIZED: return "DDERR_ALREADYINITIALIZED";
case DDERR_BLTFASTCANTCLIP: return "DDERR_BLTFASTCANTCLIP";
case DDERR_CANNOTATTACHSURFACE: return "DDERR_CANNOTATTACHSURFACE";
case DDERR_CANNOTDETACHSURFACE: return "DDERR_CANNOTDETACHSURFACE";
// etc...
}

return "(Unknown Error)";
}

This gets really messy, so I made a data table of it instead:

#define DATA_ID_STRING_NODESCR(a) { a, #a, NULL }

typedef struct _DATA_ID_STRING {
DWORD dwID;
char *pLabel;
char *pDescription;
} DATA_ID_STRING;

DATA_ID_STRING DataDDrawStringsHResult[] =
{
DATA_ID_STRING_NODESCR(DD_OK),
DATA_ID_STRING_NODESCR(DDERR_ALREADYINITIALIZED),
DATA_ID_STRING_NODESCR(DDERR_BLTFASTCANTCLIP),
// etc...
DATA_ID_STRING_NODESCR(DDERR_XALIGN)
};

and used these functions to get the error identifier:

char * GetDDReturnString(HRESULT hr)
{
return GetLabelStringByID(
hr,
DataDDrawStringsHResult,
sizeof(DataDDrawStringsHResult) / sizeof(DATA_ID_STRING),
"(Unknown Error)"
);
}

char * GetLabelStringByID(DWORD dwID, DATA_ID_STRING *pDataIDStringTable, int nEntryCount, char *szNotFoundString)
{
while(nEntryCount--)
if((pDataIDStringTable++)->dwID == dwID)
return (pDataIDStringTable - 1)->pLabel;

return szNotFoundString;
}



Share this post


Link to post
Share on other sites
Well basically I'm usuing the console window's input functions like ReadConsoleInput.


    

//This function is in my Input class

BYTE InputEngine::GetUserInput(BYTE &beMsg)
{

Flush();
//Waits 200 milliseconds and if no input sends MSG_NOTHING

if(WAIT_TIMEOUT == WaitForSingleObject(m_hInput, m_iSpeed))
return beMsg = MSG_NOTHING;

ReadConsoleInput(m_hInput, &m_irInputRecord, 1, &m_dwEvents);
if(m_irInputRecord.EventType == KEY_EVENT)
{

if(m_irInputRecord.Event.KeyEvent.bKeyDown)
{

switch(m_irInputRecord.Event.KeyEvent.wVirtualKeyCode)
{

case VK_ESCAPE:
return beMsg = MSG_QUITGAME;
break;

case VK_LEFT:
return beMsg = MSG_MOVELEFT;
break;

case VK_RIGHT:
return beMsg = MSG_MOVERIGHT;
break;
//Basically I could make a lot more cases here.


} //end of switch


} //end of if


} //end of if

return beMsg = 255;

} //GetUserInput





Then after returned I need just one more basically looks the same for the main engine to find out what to do.

[edited by - Jeff D on December 1, 2002 1:20:27 PM]

Share this post


Link to post
Share on other sites
VC++ compiler will determine what to do with a switch statement depending on what you have in it. It can do a if/elseif/else, a look-up/jump table when your cases are sequential (0,1,2,3,etc), and maybe (not sure) a translate-lookup for non-sequential (5, 100, 2040, 9) lookup.

I''ve seen the resulting assembly code for the first two situations... Don''t worry about any performance drop.

Share this post


Link to post
Share on other sites
Performance wise, the best benifit I think you can get is by looking at the cases and putting the most frequent one first, second most frequent second, and so on. This will help speed things up for many of your cases. Other than that, I don''t believe you can really get much more performance out of a switch statement, which has already been said.

Brendan

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!