Click here to Skip to main content
15,888,170 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Just a quick question.... in general (and in C# if that's relevant) is a switch more effective than a loop or the other way 'round? Thanks!
Posted
Comments
BillWoodruff 17-Jan-12 9:49am    
Well, that's a "provocative question" ... which can be a good thing ... but, I think in order to reply to this question: you need to tell us a scenario in which the purpose and behavior of the code in both the switch statement, and whatever flavor of 'loop' you are using, is identical !

And, I can't quite imagine what that would be.
Richard MacCutchan 17-Jan-12 9:59am    
Despite deleting your comments below, I don't think you do understand the basic difference. Try explaining your problem and why you think it can be solved by either a switch block or a loop.
johannesnestler 17-Jan-12 10:13am    
switch vs. loop? Are you shure you understand your question? It's like I'd ask whats more effective: cars or rain ... (There is no obvious relation - please clarify what you mean)
Hypermommy 17-Jan-12 11:33am    
Okay... so I've got this function that is currently written like this:

switch (searchFacilitator_.GroupType)
{
case BRGroupTypeIds.Audit:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.AUDIT_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.BusAcctMaster:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.BUSACCTMASTER_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.Lic:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.LIC_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.LicBill:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.LICBILL_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.TTA:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.TTA_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.TTR:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.TTR_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
case BRGroupTypeIds.TTRBill:
rowCount = mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
GroupSearchBatchStrings.TTRBILL_TEMPSPACE_NAME,
(DataAccessContext) dac_.Clone());
return rowCount;
break;
default:
return 0;
}

This feeds into a dynamically build SQL statement.

But I could write it like this:

foreach (int TempspaceName in GroupSearchBatchStrings)
{
if (TempspaceName = searchFacilitator_.GroupType)
{
mapper.ResultByRowcountForGroupSearch((searchFacilitator_.SetId, TempspaceName, dac))
}
}

Of course, I'd have to modify some of what's going on downstream to handle the fact that we're sending in an integer, not a string, but I could do that.

So, basically, I'm trying to figure out which is generally more effective, a switch or a loop.
Richard MacCutchan 17-Jan-12 11:41am    
The difference will be so small as to be unimportant and probably only measurable in micro-seconds or smaller. I would suggest you use the one that is more readable and so makes most sense to a support engineer who may have the job of maintaining it in the future.

they are 2 different things..

you can(should) use switch in order to avoid multple if-else statements

there is only one single code execution

loops like for or foreach repeteat the code n times
 
Share this answer
 
v2
Okay having read your comment, you should use a formulation that allows you not to repeat things.

What you're actually trying to do is

return mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
                                                           x,
                                                           dac);

... where x is set based on the group type.

So I recommend you set up a Dictionary<int, string> (your group types are int I think?), and populate it either statically or in the constructor of the enclosing class (probably statically if the mappings are something you can write in at compile time), e.g.:

static Dictionary<int, string> groupTypeMap = new Dictionary<int, string>();
static ClassName(){ // static constructor
 groupTypeMap[BRGroupTypeIds.Audit] = GroupSearchBatchStrings.AUDIT_TEMPSPACE_NAME;
 // etc
}


Then in the method that you're asking about, you can do

return mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId,
                                                           groupTypeMap[searchFacilitator_.GroupType,
                                                           dac);


... or, if it's possible to not be in a group and you don't want an exception from the lookup (i.e. the actual equivalent of your switch statement):


string typeName;
return groupTypeMap.TryGetValue(searchFacilitator_.GroupType, out typeName) ?
 mapper.ResultByRowcountForGroupSearch(searchFacilitator_.SetId, typename, dac) :
 0;
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900