I saw some peoples uses old and slow split.
I was interested to improve the old split to be more faster then it is now and so I did.
Here my final code:
Код:
stock split(const src[], dest[][], const delimiter = ',', size = 128)
{
new
i, li,
index;
new const
len = strlen( src );
while( ++i <= len ) if( src[ i ] == delimiter || i >= len )
{
strmid( dest[ ++index ], src, li, i, size );
li = i + 1;
}
return true;
}
If we will compare this to the old split this is used less memory end much faster for those reasons:
1. I not checking every round the length (twice!) like the old split.
2. I does not have len variable (for none reason).
3. I not storing data that for no reason.
If you want some speed comparison so I did it for you, here the final result:
Looping 100,000 times:
Mine split - 2017 - Just two seconds
Old split - 14317 - Just over 10 seconds
Looping 1,000,000 times:
Mine split - 20213 - Just 20 seconds
Old split - 143105 - Just over 2 minutes
The code that I used to those tests:
Код:
new
t0, t1, t2,
test[4][128];
t0 = GetTickCount();
for (new i = 0; i < LLOP; i++ )
{
split("test|is|mine|test", test, '|');
}
t1 = GetTickCount();
printf("< speed > mine split: %d", t1 - t0);
for (new i = 0; i < LLOP; i++ )
{
split2("test|is|mine|test", test, '|');
}
t2 = GetTickCount();
printf("< speed > frenzys split: %d", t2 - t1);
///////////////////////////////////////////////////////////////////////////
stock split(const src[], dest[][], const delimiter = ',', size = 128)
{
new
i, li,
index;
new const
len = strlen( src );
while( ++i <= len ) if( src[ i ] == delimiter || i >= len )
{
strmid( dest[ ++index ], src, li, i, size );
li = i + 1;
}
return true;
}
stock split2(const strsrc[], strdest[][], delimiter)
{
new i, li;
new aNum;
new len;
while(i <= strlen(strsrc))
{
if(strsrc[i] == delimiter || i == strlen(strsrc))
{
len = strmid(strdest[aNum], strsrc, li, i, 128);
strdest[aNum][len] = 0;
li = i+1;
aNum++;
}
i++;
}
return 1;
}
Enjoy of using this, all rights for the PAWN split goes to the old split creator - I only improved this.