Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
- while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
- __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
- GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
- #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
- %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
- stock strreplacechar_1(string[], oldchar, newchar)
- {
- new matches;
- if(ispacked(string)) {
- if(newchar == '\0') {
- for(new i; string{i} != '\0'; i++) {
- if(string{i} == oldchar) {
- strdel(string, i, i + 1);
- matches++;
- }
- }
- } else {
- for(new i; string{i} != '\0'; i++) {
- if(string{i} == oldchar) {
- string{i} = newchar;
- matches++;
- }
- }
- }
- } else {
- if(newchar == '\0') {
- for(new i; string[i] != '\0'; i++) {
- if(string[i] == oldchar) {
- strdel(string, i, i + 1);
- matches++;
- }
- }
- } else {
- for(new i; string[i] != '\0'; i++) {
- if(string[i] == oldchar) {
- string[i] = newchar;
- matches++;
- }
- }
- }
- }
- return matches;
- }
- stock strreplacechar_2(szString[], iOldChar, iNewChar, bool:bIgnoreCase = false) {
- new
- szSearchString[2],
- bool:bIsPacked = ispacked(szString),
- iPos = 0,
- iMatches = 0
- ;
- szSearchString[0] = iOldChar;
- while (-1 != (iPos = strfind(szString, szSearchString, bIgnoreCase, iPos))) {
- if (bIsPacked)
- szString{iPos++} = iNewChar;
- else
- szString[iPos++] = iNewChar;
- iMatches++;
- }
- return iMatches;
- }
- public OnFilterScriptInit() {
- START_BENCH(1000);
- new
- szString[] = "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
- ;
- strreplacechar_1(szString, 'o', '0');
- FINISH_BENCH("strreplacechar_1");
- START_BENCH(1000);
- new
- szString[] = "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
- ;
- strreplacechar_2(szString, 'o', '0');
- FINISH_BENCH("strreplacechar_2");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement