Re: What's the most efficient testing of variables?
- From: Joseph M. Newcomer <newcomer@xxxxxxxxxxxx>
- Date: Wed, 04 Jan 2006 18:12:37 -0500
Depending on the hashing function used, a hash table can usually work somewhere between
O(1) and O(2), that is, constant time. If there is a known set of values, an array of
pointers gives O(1) as well.
joe
On Wed, 04 Jan 2006 10:45:37 -0600, "Doug Harrison [MVP]" <dsh@xxxxxxxx> wrote:
>On Wed, 4 Jan 2006 08:26:04 -0600, "Jeff" <jbk@xxxxxxxxxxxx> wrote:
>
>>I need to quickly (efficiently) compare a DWORD with a list of DWORDs to
>>find the one and only match. I can guaranty that there is only one match.
>>switch won't work because both are variables. How about if...else if...else
>>if.... etc? Or is there a better way?
>>
>>
>>DWORD dwPassed, dwAssigned1, dwAssigned2, dwAssigned3;
>>
>>ideally I would use a switch like statement:
>>
>>switch(dwPassed)
>>{
>>case dwAssigned1
>> deal with it;:
>> break;
>>case dwAssigned2:
>> deal with it;:
>> break;
>>case dwAssigned3:
>> deal with it;:
>> break;
>>default:
>> deal with it;:
>> break;
>>}
>>
>>But since all are variables it won't work.
>
>There's nothing wrong with if/else if/else, except it doesn't scale too
>well. The general solution is to map these values to functions, so that you
>can look up a value and call the associated function. You can do this
>easily with std::map (O(log N)), though for relatively small data sets,
>linear search of an array (O(N)) will be faster.
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.
- References:
- What's the most efficient testing of variables?
- From: Jeff
- Re: What's the most efficient testing of variables?
- From: Doug Harrison [MVP]
- What's the most efficient testing of variables?
- Prev by Date: Re: How to print a CStatic control ?
- Next by Date: Re: Which string to use?
- Previous by thread: Re: What's the most efficient testing of variables?
- Next by thread: Re: What's the most efficient testing of variables?
- Index(es):
Relevant Pages
|