Click here to Skip to main content
15,868,141 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Performance problem with my FindOneOf function Pin
Ivan Ivanov 8323-Jun-09 0:32
Ivan Ivanov 8323-Jun-09 0:32 
GeneralRe: Performance problem with my FindOneOf function Pin
Stuart Dootson23-Jun-09 0:41
professionalStuart Dootson23-Jun-09 0:41 
GeneralRe: Performance problem with my FindOneOf function Pin
Ivan Ivanov 8323-Jun-09 4:17
Ivan Ivanov 8323-Jun-09 4:17 
GeneralRe: Performance problem with my FindOneOf function Pin
Ivan Ivanov 8324-Jun-09 1:11
Ivan Ivanov 8324-Jun-09 1:11 
AnswerRe: Performance problem with my FindOneOf function Pin
Skippums22-Jun-09 9:02
Skippums22-Jun-09 9:02 
GeneralRe: Performance problem with my FindOneOf function Pin
Stuart Dootson22-Jun-09 9:34
professionalStuart Dootson22-Jun-09 9:34 
GeneralRe: Performance problem with my FindOneOf function Pin
Ivan Ivanov 8322-Jun-09 11:28
Ivan Ivanov 8322-Jun-09 11:28 
AnswerRe: Performance problem with my FindOneOf function Pin
Roger Stoltz22-Jun-09 9:22
Roger Stoltz22-Jun-09 9:22 
I think Stuart's advice to use the C runtime in this case is the best alternative.

An algorithm can be optimized for different purposes and can also be better or worse in some scenarios. I guess the standard C runtime implementation is rather generic and predictable.

Try out the following algorithm and you'll see what I mean.
It is twice as fast as CString::FindOneOf() with the strings you're using for testing and measurement when it's release-built. On the other hand, adding a 'u' in the search string will make the algorithm 50% slower than CString::FindOneOf(). Surely you can optimize it further, but it will still have its weaknesses.
The key in the algorithm below is to do as little as possible with each char in the string to be searched.
int FindOneOf( const char* pString, const char* pSearch )
{
    register char cMask = -1;
    register char cPattern = *pSearch;
    register int nPos;
    int nSPos;
    int nRet = -1;

    for( nPos = 1; pSearch[nPos]; ++nPos )
    {
        cMask &= ~(pSearch[nPos - 1] ^ pSearch[nPos]);
        cPattern |= pSearch[nPos];
    }
    // cMask now contains '1's in bits that have the same value for every char in pSearch string

    // Check each char in pString
    for( nPos = 0; pString[nPos]; ++nPos )
    {
        // Find out which bits have the same values by '~(pString[nPos] ^ cPattern)'
        // Mask the bits of interest and compare with the mask
        if( (~(pString[nPos] ^ cPattern) & cMask) == cMask )
        {
            // We have a possible match, now walk through the search string
            for( nSPos = 0; pSearch[nSPos]; ++nSPos )
            {
                if( pString[nPos] == pSearch[nSPos] )
                {
                    // A match was found, update return value and get out
                    nRet = nPos;
                    break;
                }
            }
            if( nRet != -1 )
            {
                break;
            }
        }
    }

    return nRet;
}

Beware! The above is just an example at the top of my head! Wink | ;)


"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown


GeneralRe: Performance problem with my FindOneOf function Pin
Ivan Ivanov 8322-Jun-09 10:59
Ivan Ivanov 8322-Jun-09 10:59 
GeneralRe: Performance problem with my FindOneOf function Pin
Roger Stoltz22-Jun-09 21:39
Roger Stoltz22-Jun-09 21:39 
GeneralRe: Performance problem with my FindOneOf function [modified] Pin
Ivan Ivanov 8324-Jun-09 1:45
Ivan Ivanov 8324-Jun-09 1:45 
GeneralRe: Performance problem with my FindOneOf function Pin
Roger Stoltz24-Jun-09 4:16
Roger Stoltz24-Jun-09 4:16 
GeneralRe: Performance problem with my FindOneOf function [modified] Pin
Ivan Ivanov 8325-Jun-09 1:25
Ivan Ivanov 8325-Jun-09 1:25 
QuestionXMLHTTP post failed with status code 415 Pin
Ash_VCPP22-Jun-09 3:47
Ash_VCPP22-Jun-09 3:47 
AnswerRe: XMLHTTP post failed with status code 415 Pin
KarstenK22-Jun-09 4:15
mveKarstenK22-Jun-09 4:15 
QuestionTimer on Statusbar Pin
susanne122-Jun-09 3:10
susanne122-Jun-09 3:10 
AnswerRe: Timer on Statusbar Pin
Rajesh R Subramanian22-Jun-09 3:25
professionalRajesh R Subramanian22-Jun-09 3:25 
AnswerRe: Timer on Statusbar Pin
David Crow22-Jun-09 4:05
David Crow22-Jun-09 4:05 
QuestionHow to access element in Multiset by index Pin
alikalik22-Jun-09 3:05
alikalik22-Jun-09 3:05 
AnswerRe: How to access element in Multiset by index Pin
Chris Losinger22-Jun-09 3:13
professionalChris Losinger22-Jun-09 3:13 
GeneralRe: How to access element in Multiset by index Pin
alikalik22-Jun-09 3:30
alikalik22-Jun-09 3:30 
AnswerRe: How to access element in Multiset by index Pin
Sarath C22-Jun-09 3:20
Sarath C22-Jun-09 3:20 
Questionsplit wavefiles programetically Pin
N Vamshi Krishna22-Jun-09 3:04
N Vamshi Krishna22-Jun-09 3:04 
AnswerRe: split wavefiles programetically Pin
Rozis23-Jun-09 6:42
Rozis23-Jun-09 6:42 
QuestionPlease clarify my doubt.... Pin
Rakesh522-Jun-09 2:25
Rakesh522-Jun-09 2:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.