Vincent Fatica
2010-06-11 18:23:01 UTC
In a ReadFile() loop I have this code for counting newlines.
for ( BYTE* p=inBuf; p<pEnd; p += dwCharSize )
if ( (bUnicode && *((WCHAR*)p) == L'\n') || (!bUnicode && *((CHAR*)p) ==
'\n') )
dwLineCount += 1;
In my performance tests, I use a non-Unicode file with lines of about 100
characters. In each test, three BOOLs are evaluated ... bUnicode will be FALSE,
!bUnicode will be TRUE, and then the character will be tested.
I would expect the following criterion to be faster
( (bUnicode && *((WCHAR*)p) == L'\n') || (*((CHAR*)p) == '\n' && !bUnicode) )
because in the typical case (character not newline) only two evaluations should
be done ... bUnicode will be FALSE and the character won't be a newline. But in
fact this alternative (the only change) increases the time by 50%.
This shakes my confidence a bit because I have always ordered logical tests in a
way which (I thought) would get them done the fastest. ... any ideas what's
going on here?
Thanks.
for ( BYTE* p=inBuf; p<pEnd; p += dwCharSize )
if ( (bUnicode && *((WCHAR*)p) == L'\n') || (!bUnicode && *((CHAR*)p) ==
'\n') )
dwLineCount += 1;
In my performance tests, I use a non-Unicode file with lines of about 100
characters. In each test, three BOOLs are evaluated ... bUnicode will be FALSE,
!bUnicode will be TRUE, and then the character will be tested.
I would expect the following criterion to be faster
( (bUnicode && *((WCHAR*)p) == L'\n') || (*((CHAR*)p) == '\n' && !bUnicode) )
because in the typical case (character not newline) only two evaluations should
be done ... bUnicode will be FALSE and the character won't be a newline. But in
fact this alternative (the only change) increases the time by 50%.
This shakes my confidence a bit because I have always ordered logical tests in a
way which (I thought) would get them done the fastest. ... any ideas what's
going on here?
Thanks.
--
- Vince
- Vince