istrcmp(str[], str2[], bool: per = true){
if(strlen(str) > strlen(str2)) return -1;
if(strlen(str) < strlen(str2)) return 1;
if(!per){
for(new i; str[i]; ++i){
if(str[i] != str2[i]){
return false;
}
}
}else{
for(new i; str[i]; ++i){
if(tolower(str[i]) != (str2[i])){
return false;
}
}
}
return true;
}
[02:50:42] Bench for SA-MP: executes, by average, 352.10 times/ms. [02:50:44] Bench for SuYaNw: executes, by average, 1892.96 times/ms.
#include a_samp
#pragma tabsize 0
#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));}
#define F1 "Que cor й os cabelos brancos de maria"
#define F2 "Que cor й os cabelos pretos de maria"
main()
{
START_BENCH(1000);
if(strcmp(F1, F2, true) == 0){
}
FINISH_BENCH("SA-MP");
START_BENCH(1000);
if(istrcmp(F1, F2) == 0){
}
FINISH_BENCH("SuYaNw");
}
istrcmp(str[], str2[], bool: per = true){
if(strlen(str) > strlen(str2)) return -1;
if(strlen(str) < strlen(str2)) return 1;
if(!per){
for(new i; str[i]; ++i){
if(str[i] != str2[i]){
return false;
}
}
}else{
for(new i; str[i]; ++i){
if(tolower(str[i]) != tolower(str2[i])){
return false;
}
}
}
return true;
}
// case true pra string pequenas iguais // case false pra string pequenas iguais // case false pra string pequenas diferentes // case true para strings grandes e iguais // case true para strngs de tamanhos diferentes // case false para strngs de tamanhos diferentes // case false para strings grandes e iguais |
new maxstring[999] = {'x', ...};
START_BENCH(1000);
istrcmp("bru", "bru", false);
istrcmp("bru", "bru", true);
istrcmp("bru", "BRU", false);
istrcmp("bru", "bruno", false);
istrcmp("bru", "bruno", true);
istrcmp(maxstring, maxstring, true);
istrcmp(maxstring, maxstring, false);
FINISH_BENCH("SuyanW");
START_BENCH(1000);
strcmp("bru", "bru", false);
strcmp("bru", "bru", true);
strcmp("bru", "BRU", false);
strcmp("bru", "bruno", false);
strcmp("bru", "bruno", true);
strcmp(maxstring, maxstring, false);
strcmp(maxstring, maxstring, true);
FINISH_BENCH("Kye");
[02:57:30] Bench for SuyanW: executes, by average, 1.93 times/ms. [02:57:32] Bench for Kye: executes, by average, 3.55 times/ms.
[02:56:55] Bench for SuyanW: executes, by average, 127.11 times/ms. [02:56:57] Bench for Kye: executes, by average, 508.97 times/ms.
Consegui, um em mйdia 30 vezes mais veloz:
pawn Код:
|
istrcmp(str[], str2[], bool: per = true){
static
a,
n
;
a = strlen(str);
n = strlen(str2);
if(a > n) return -1;
if(a < n) return 1;
for(new i; str[i]; ++i) if(tolower(str[i]) != tolower(str2[i])) return 1;
return 0;
}
if(__len != _len) if(__len > _len) return -1; else return 1;
if(a > n) return -1;
if(a < n) return 1;
Seu erro й nas comparaзхes do strlen.
Veja a minha: pawn Код:
pawn Код:
Outro truque, foi a comparaзхes dos valores, eu nгo sabia, mas !(var1 - var2) й mais rбpido que var1 == var2 ![]() ![]() |
new maxstring[999] = {'x', ...}; START_BENCH(5000); istrcmp("bru", "bru", false); istrcmp("bru", "bru", true); istrcmp("bru", "BRU", false); istrcmp("bru", "bruno", false); istrcmp("bru", "bruno", true); istrcmp(maxstring, maxstring, false); istrcmp(maxstring, maxstring, true); FINISH_BENCH("Bruno"); START_BENCH(5000); strcmp("bru", "bru", false); strcmp("bru", "bru", true); strcmp("bru", "BRU", false); strcmp("bru", "bruno", false); strcmp("bru", "bruno", true); strcmp(maxstring, maxstring, false); strcmp(maxstring, maxstring, true); FINISH_BENCH("Kye");