str_stri surcharge

Posted By: txesmi

str_stri surcharge - 02/28/15 11:53

Hi,
I needed to intensively use str_stri for a osm hypertext parser I am writing and I realized that it got slower the bigger the parsed strings or char buffers were. In the try of indetifying the problem I substituted all the str_stri calls by my own function and surprisingly it solved the problem. I could not believe it.

Click to reveal..

Code:
var str_stri_custom ( char *chr1, char *chr2 )
{
	var pos = 0;
	while ( *chr1 != NULL )
	{
		pos += 1;
		if ( *chr1 == *chr2 )
		{
			char *chr1b = chr1 + 1;
			char *chr2b = chr2 + 1;
			while ( ( *chr1b != NULL ) && ( *chr2b != NULL ) )
			{
				if ( *chr1b != *chr2b )
					break;
				chr1b += 1;
				chr2b += 1;
			}
			if ( *chr2b == NULL )
				return pos;
		}
		chr1 += 1;
	}
	return 0;
}


Posted By: FBL

Re: str_stri surcharge - 03/02/15 11:01

Did you also try in published mode?

I don't know the engine code - but for sure there's a certain chance that in development mode there are debug routines active for detecting array out of bounds and the like.
Posted By: jcl

Re: str_stri surcharge - 03/02/15 14:39

There is some overhead in development mode, but it should not make much difference. str_stri is in fact very slow, as far as I can see in the source code. Your function is much faster. I don't see at a first glance why str_stri is coded this way, maybe it's due to some special case handling. As long as you don't use unicode or some STRING specific stuff I recommend that you continue with your own function.
Posted By: txesmi

Re: str_stri surcharge - 03/03/15 12:28

Thank you for your answers!
I will go this way. Maybe a chr_cmp is a good choice too.

Salud!
© 2024 lite-C Forums