[Off] strcmp veloz
#1

Vou fazer um desafio, eu particularmente nгo consegui fazer muita coisa.


Faзa um cуdigo, que fique com velocidade superior ao strcmp nativo da SA:MP
Reply
#2

Minha versгo:
pawn Код:
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;
}
Inclue todas as funзхes da strcmp nativa.


Testes:
Код:
[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.
Reply
#3

Posta o cуdigo dos testes pra eu analisar
Reply
#4

pawn Код:
#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;
}
Reply
#5

Nгo. Tem que fazer o teste em todas as escalas

Quote:

// 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

pawn Код:
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.
Reprovada para quase todos casos. Exceto em strings de tamanhos diferentes. (Meio obvio, logo que tem um strlen ali de cara )

Se tiver outros cуdigos manda aн.
Reply
#6

http://forum.sa-mp.com/showpost.php?...0&postcount=20
Reply
#7

Quote:
Originally Posted by ipsBruno
Посмотреть сообщение
Consegui, um em mйdia 30 vezes mais veloz:

pawn Код:
fstrcmp(str[], str2[], bool: cases = true){

    static __len, _len;
   
    __len = strlen(str);
    _len = strlen(str2);
   
    if(__len != _len) if(__len > _len) return -1; else return 1;

    switch(cases){
        case 0: {
            for( ; --__len > -1 ; ) {
                if(!(str[__len] - str2[__len])) return -2;
            }
        }
        default: {
            for( ; --__len > -1 ; ) {
                if(!(tolower(str[__len]) - tolower(str2[__len]))) return -2;
            }
        }
    }
    return 0;
}
Por enquanto estб comparando certo. Resta saber se os testes estгo dizendo a verdade
Seu sem graзa


eu adicionando um simples loop jб fica fica 500% mais lento que o nativa!

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;
}
#EDIT:

seus testes:
Reply
#8

Seu erro й nas comparaзхes do strlen.

Veja a minha:

pawn Код:
if(__len != _len) if(__len > _len) return -1; else return 1;
veja a sua
pawn Код:
if(a > n) return -1;
    if(a < n) return 1;
O meu, sу faz uma condiзгo as vezes, o seu faz sempre 2 codiзхes. Lуgica

Outro truque, foi a comparaзхes dos valores, eu nгo sabia, mas !(var1 - var2) й mais rбpido que var1 == var2
Reply
#9

Quote:
Originally Posted by ipsBruno
Посмотреть сообщение
Seu erro й nas comparaзхes do strlen.

Veja a minha:

pawn Код:
if(__len != _len) if(__len > _len) return -1; else return 1;
veja a sua
pawn Код:
if(a > n) return -1;
    if(a < n) return 1;
O meu, sу faz uma condiзгo as vezes, o seu faz sempre 2 codiзхes. Lуgica

Outro truque, foi a comparaзхes dos valores, eu nгo sabia, mas !(var1 - var2) й mais rбpido que var1 == var2
Parabйns, mostrou seu poder alienнgena novamente ...


Crie outro desafio desses e.e
Reply
#10

Cara, MAS EU NГO SEI porque a funзгo ficou tгo rбpida. Aqueles truques deveriam melhor em 2 ou 3 exeuзхes por ms, nгo 80 o.O

Faz os testes ai e vк se a funзгo tб certa, aqui deu tudo certo.

Estranho


Код:
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");
Usei isto pra testes
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)