Re: [Libclc-developers] new function: clc_strrev
Status: Planning
Brought to you by:
augestad
|
From: <bo...@me...> - 2003-03-23 14:50:59
|
Hallvard B Furuseth wrote:
> Bjørn Augestad writes:
>
>>Some sales pitches for clc_strrev. :-)
>
>
> OK.
>
> In that case, here is a smaller version. I added clc_strnrev() too
> since I imagine that can be useful sometimes, or at least faster when
> we already know strlen.
How about renaming clc_strnrev() to clc_memrev()? That's what it does
and suddenly we have a general purpose memory reverser. It can even be
used to swap bytes in integers.
>
> BTW, I did as you said and replaced #include "clc_*" with <clc_*>:-)
That was in the user documentation. We must use "" to avoid that we
include headers from a previous version already installed. Nice try,
though ;-)
>
> Note: The 'if(len)' is a very slight optimization, it can be omitted
> (and the end-- moved) at the price of an extra loop for uneven-length
> strings.
>
> /* $Id$ */
> /*
> * Copyright(c) 2003, Hallvard B Furuseth <h.b...@us...>
I compared the two versions and optimized mine a little. Turned out that
mine was slightly faster on a 600MHz Pentium running Linux. Then I tried
it on an Athlon 1800+ running Windows XP. Then your version was faster.
Finally Windows *crashed*. :-( Oh well, your version looks better so we
keep that one. Please update libclc/src/string/clc_strrev.c
Speaking of optimizations, any opinions on code like this?
int clc_ultostr(char *ptr, size_t size, unsigned long num, int base)
{
const char *sym = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char* sp;
#ifdef CLC_FAST
if(base == 2) {
sp = ptr;
size--;
do {
*sp++ = num & 0x01 ? '1' : '0';
num >>= 1;
} while(num > 0 && size-- > 0) ;
if(num == 0) {
*sp = '\0';
clc_strrev(ptr);
}
return num == 0;
}
#endif
.... /* regular version goes here */
}
The CLC_FAST version is 4 times faster than the regular one, but the
library will be bigger. Nice for those who likes to print bitpatterns?
Strangely enough division by bitshifting is still faster than / 2. (gcc
3.2.1 -O3 -NDEBUG)
--
boa
Please join the libclc-developers list
at http://lists.sourceforge.net/lists/listinfo/libclc-developers
|