27.06.2011, 16:08
pawn Code:
stock str_replace (newstr [], oldstr [], srcstr [], deststr [], bool: ignorecase = false, size = sizeof (deststr))
{
new
newlen = strlen (newstr),
oldlen = strlen (oldstr),
srclen = strlen (srcstr),
idx,
rep;
for (new i = 0; i < srclen; ++i)
{
if ((i + oldlen) <= srclen)
{
if (!strcmp (srcstr [i], oldstr, ignorecase, oldlen))
{
deststr [idx] = '\0';
strcat (deststr, newstr, size);
++rep;
idx += newlen;
i += oldlen - 1;
}
else
{
if (idx < (size - 1))
deststr [idx++] = srcstr [i];
else
return rep;
}
}
else
{
if (idx < (size - 1))
deststr [idx++] = srcstr [i];
else
return rep;
}
}
deststr [idx] = '\0';
return rep;
}
I've also compared your version (strreplace2) and mine (str_replace) and the strreplace from DUtils.
pawn Code:
for (new i = 0; i < 500; ++i)
{
new
t,
string [256] = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe",
string2 [256];
const MAX_I = 20000;
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
string2 = strreplace ("Affe", "Kuh", string);
}
t = GetTickCount () - t;
printf ("strreplace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
str_replace ("Kuh", "Affe", string, string2);
}
t = GetTickCount () - t;
printf ("str_replace: %d ms (%s)", t, string2);
t = GetTickCount ();
for (new k = 0; k < MAX_I; ++k)
{
string = "AffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffeAffe";
strreplace2 (string, "Affe", "Kuh");
}
t = GetTickCount () - t;
printf ("strreplace2: %d ms (%s)", t, string);
}
Code:
[18:09:33] strreplace: 8101 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:34] str_replace: 379 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:34] strreplace2: 547 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:42] strreplace: 7986 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:43] str_replace: 378 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:43] strreplace2: 550 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:51] strreplace: 8017 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:52] str_replace: 376 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:09:52] strreplace2: 549 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:00] strreplace: 8003 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:00] str_replace: 382 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh) [18:10:01] strreplace2: 546 ms (KuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuhKuh)
The reason why mine is the fastest is:
My str_replace iterates only once through the whole string because it doesn't use strfind.
strfind is a bad choice here because it also iterates through the whole string in the worst case and for every match it has the iterates once more.
For many matches this is not a good choice.