Hi All
I'm developing my own CString class. With the idea to make it "much" faster than the standard CString, easily upgradeable, and to include into it some non-standard parsing functions I often use in my work, those will be much better to be part of the CFString object itself, instead of being separate functions.
And in deed I have some noticeable achievements for the basic functions my CFString uses:
My copy() is 35% faster then strcpy()!
My lengh() is 30% faster than strlen().
BUT note that the optimizer often pre-calculates strlen() calls and replaces them with respective numbers!
For example int i = strlen("abc") will eventually generate mov EAX, 3 instaed of generating the asm code of strlen() itself, that happens for hardcoded strings! so in the cases when strlen() isn't replaced by a number, my function is 30% faster
My compare() is 20% faster than strcmp().
I used the inline assembly option of the Visual C++ compiler, the "__asm" command to achieve this.
I also developed an equalizing mechanism, so when I write str1 = str2 the "operator =" actually doesn't copy the data from str2 to str1
the copy occurs ONLY in specific cases when it's needed.
And that mechanism provides about 35 - 40% faster equalizing betweend CFString objects in comparison to the CString equalizing!
So far so good! But in the case with FindOneOf the standard CString beated me badly! CString::FindOneOf is almost twise faster than my CFString::FindOneOf, and my FindOneOf IS written in assembly too. I did everithing I could to tweak it up, but I only reduced the time from 33000ms to 28000ms and CString::FindOneOf does the job for 18000ms, and my function suppose to be faster, now I'll be happy just to equal the score of CString::FindOneOf!
This is the test code I use:
<br /> CString/CFString str1("abcdefghijklmnoprs0987654321");<br /><br /> int start = GetTickCount();<br /> for(int pos = 0; pos; 100000000; pos++)<br /> {<br /> index = 0;<br /> index = str1.FindOneOf("1234567890");<br /> }<br /> int time = GetTickCount() - start;<br /><br /> CString strTime;<br /> strTime.Format("%d, %d", time, index);<br /> m_editResult.SetWindowText(strTime);
I looked at the disassembly of the CString::FindOneOf but couldn't find a single loop, I didn't understan anything! there are a lot of stack operations (PUSH POP) a lot of CALLs and they suppose to be slow, I don't have any idea how can this function beat mine!!!
This is the algorithm I use written in C, although it's written in assembly and I believe optimised in the actual function:
<br /> const char* pstrTBuffer = m_pstrBuffer;
In case the code isn't clear enough:
The idea is as simple as possible, compare each character in the main string m_pstrBuffer with each character in the charset pstrSeek
if there is a match return its index if not return -1
I assumed that the problem isn't in my coding technique, it must be generally in the algorithm I use!!!
Does somebody know a better algorithm to implement FindOneOf??? :)
I will appreciate any help - thank you!!! :)
Sorry for the prolonged question, but I wanted to be as clear as I can in my description.
modified on Monday, June 22, 2009 10:20 AM