28.01.2011, 11:23
(
Последний раз редактировалось Slice; 28.01.2011 в 13:31.
)
Crap! I was just making a post on how using strfind would be faster.
Anyways, benchmarking:
Edit: Calgon's countControlCharacters function gave me an idea, so I made these:
Usage:
Code:
You can probably tell I've been doing too much JavaScript lately by looking at the for-loops. lol.
Anyways, benchmarking:
Код:
Bench for Looping the string: executes, by average, 159 times/ms (159164 times, 1000 ms). Bench for Using strfind: executes, by average, 825 times/ms (825567 times, 1000 ms).
Usage:
pawn Код:
CountChars( haystack[], needless[] ); // Counts each char in the needless string and returns the number of matches.
CountMatches( haystack[](, needle[], ...) ); // Counts the string(s) passed after the haystack - it's possible to match up to 62 strings.
You can probably tell I've been doing too much JavaScript lately by looking at the for-loops. lol.
pawn Код:
stock CountChars( const szString[ ], const szChars[ ] )
{
new iCount = 0, iPos;
for ( new szSearchChar[ 2 ], i = 0; ( szSearchChar[ 0 ] = szChars[ i ] ); i++ )
{
iPos = -1;
while ( ( iPos = strfind( szString, szSearchChar, false, ++iPos ) ) != -1 )
++iCount;
}
return iCount;
}
stock CountMatches( const szString[ ], bool:bIgnoreCase = false, ... )
{
new iMatches, szBuffer[ 64 ], i, iArgs, c, j, iPos;
iMatches = 0;
iArgs = numargs( );
for ( i = 2; i < iArgs; i++ )
{
for ( j = 0; ( c = getarg( i, j ) ); j++ )
szBuffer[ j ] = c;
szBuffer[ j + 1 ] = 0;
iPos = -1;
while ( ( iPos = strfind( szString, szBuffer, bIgnoreCase, ++iPos ) ) != -1 )
++iMatches;
}
return iMatches;
}
public OnFilterScriptInit( )
{
printf( "%d", CountChars( "I\nLike\nNew\nLines\n:D.\t\t\t\ttabs!", "\n\t" ) );
printf( "%d", CountMatches( "I like to repeat myself. I like to repeat myself. I like to repeat myself.", false, "I", "repeat" ) );
new iMatches = CountMatches( "Fuckin.. What the fuckin fuck?! Who the fuck fucked this fucking...?? How did you two fucking fucks...?! FUCK!!", false, "fuck" );
if ( iMatches > 5 )
printf( "Enough cursing!!" );
}