Advertisement
RyDeR_

Benchmark (strfind vs. loop)

Oct 31st, 2011
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 2.62 KB | None | 0 0
  1. #define START_BENCH(%0); {new __a=%0,__b=0,__c,__d=GetTickCount(),__e=1;do{}\
  2.     while(__d==GetTickCount());__c=GetTickCount();__d=__c;while(__c-__d<__a||\
  3.     __e){if(__e){if(__c-__d>=__a){__e=0;__c=GetTickCount();do{}while(__c==\
  4.     GetTickCount());__c=GetTickCount();__d=__c;__b=0;}}{
  5.  
  6. #define FINISH_BENCH(%0); }__b++;__c=GetTickCount();}printf(" Bench for "\
  7.     %0": executes, by average, %.2f times/ms.",floatdiv(__b,__a));}
  8.  
  9. stock strreplacechar_1(string[], oldchar, newchar)
  10. {
  11.     new matches;
  12.     if(ispacked(string)) {
  13.         if(newchar == '\0') {
  14.             for(new i; string{i} != '\0'; i++) {
  15.                 if(string{i} == oldchar) {
  16.                     strdel(string, i, i + 1);
  17.                     matches++;
  18.                 }
  19.             }
  20.         } else {
  21.             for(new i; string{i} != '\0'; i++) {
  22.                 if(string{i} == oldchar) {
  23.                     string{i} = newchar;
  24.                     matches++;
  25.                 }
  26.             }
  27.         }
  28.     } else {
  29.         if(newchar == '\0') {
  30.             for(new i; string[i] != '\0'; i++) {
  31.                 if(string[i] == oldchar) {
  32.                     strdel(string, i, i + 1);
  33.                     matches++;
  34.                 }
  35.             }
  36.         } else {
  37.             for(new i; string[i] != '\0'; i++) {
  38.                 if(string[i] == oldchar) {
  39.                     string[i] = newchar;
  40.                     matches++;
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     return matches;
  46. }
  47.  
  48. stock strreplacechar_2(szString[], iOldChar, iNewChar, bool:bIgnoreCase = false) {
  49.         new
  50.                      szSearchString[2],
  51.                 bool:bIsPacked = ispacked(szString),
  52.                      iPos = 0,
  53.                      iMatches = 0
  54.         ;
  55.        
  56.         szSearchString[0] = iOldChar;
  57.        
  58.         while (-1 != (iPos = strfind(szString, szSearchString, bIgnoreCase, iPos))) {
  59.                 if (bIsPacked)
  60.                         szString{iPos++} = iNewChar;
  61.                 else
  62.                         szString[iPos++] = iNewChar;
  63.                
  64.                 iMatches++;
  65.         }
  66.        
  67.         return iMatches;
  68. }
  69.  
  70. public OnFilterScriptInit() {  
  71.     START_BENCH(1000);
  72.        
  73.     new
  74.         szString[] = "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
  75.     ;
  76.     strreplacechar_1(szString, 'o', '0');
  77.    
  78.     FINISH_BENCH("strreplacechar_1");
  79.        
  80.     START_BENCH(1000);
  81.        
  82.     new
  83.         szString[] = "oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo"
  84.     ;
  85.     strreplacechar_2(szString, 'o', '0');
  86.    
  87.     FINISH_BENCH("strreplacechar_2");
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement