[Off] strdel e strins veloz
#1

Mais um desafio! Dessa vez, a funзгo deverб ser parecida com strdel, mas deve deletar com mais velocidade

Mгos a obra
Reply
#2

Consegui uma 4 vezes mais lenta. Mas acho que da pra fazer algo melhor com loops

pawn Код:
fstrdel(string[], start, end = sizeof string, size = sizeof string) {

    strcat((buffer[0] = 0, buffer), string, size);

    buffer[start] = EOS;

    strcat(buffer, string[end], size);

    strcat((string[0] = EOS, string), buffer, size);
    return true;

}
pawn Код:
/*
        new string2[] = " й um l";

        new string1[66];

        string1 = "bruno indo sabe tudo";

        fstrins(string1, string2, 6);
       
        print(string1);
*/

fstrins(string[], append[], pos, size = sizeof string, sizes = sizeof append) {

    format(buffer, 0xff, "%s", string);
   
    buffer[pos] = 0;
   
    memcpy(buffer, append, strlen(buffer) * 4, sizes * 4, size);
   
    strcat(buffer, string[pos]);
    strcat((string[0]=0,string), buffer, size);
   
    return true;
   
}
Reply
#3

Voce e suas velocidades xD. To sem tempo. Mals. Flw ai
Reply
#4

Bom, dб pra deletar a parte final de uma string assim (Eu acho que deleta)

pawn Код:
stock delendstr(source[144],start)
{
    source[start] = EOS;
    return source;
}
Agora como deletar do meio, vou tentar hoje a noite
Reply
#5

Sobre o strdel eu fiz 4 versхes e a velocidade tem vindo a aumentar.

pawn Код:
#define MAX_STRING (128)
   
stock bstrdel(string[], start, end, size = sizeof string)
{
    static buffer[MAX_STRING];
    strmid(buffer,string,0,(start-1),size);
    strmid(string,string,end,size,size);
    strins(string,buffer,0,size);
    buffer[0] = EOS;
}

static buffer2[MAX_STRING];
static buffer3[MAX_STRING];

stock bstrdel2(string[], start, end, size = sizeof string)
{
    strmid(buffer2, string, end, size, size);
    string[start] = EOS;
    strins(string, buffer2, start-1, end);
}

stock bstrdel3(string[], start, end, size = sizeof string)
{
    strmid(buffer3, string, (end + 1), size, size);
    string[start] = EOS;
    strcat(string,buffer3,size);
}

stock bstrdel4(string[], start, end)
{

    while(start != end)
    {
        string[start] = EOS;
        start++;
    }
}
Eu cheguei ao ponto de criar uma variavel fora da funзгo para adquirir mais velocidade, eis os testes usando a bench mark do slice.

Код:
[22:39:05] Bench for sa-mp: executes, by average 575.58 times/ms.
[22:39:05] Bench for b1: executes, by average, 401.24 times/ms.
[22:39:05] Bench for b2: executes, by average, 430.16 times/ms
[22:39:05] Bench for b3: executes, by average,410.03 times/ms
[22:39:05] Bench for b4: executes, by average, 488.89 times/ms
Sobre os testes eu foquei-me mais em resolver strings que necessitam de um strdel no meio, ex: Hello my World, vocк quer tirar esse my entгo foi isso que eu fiz, assim й bastante mais eficaz porque se funciona com caracteres no meio funcionarб no fim.

cуdigo que fiz para as benchmark:

pawn Код:
#include <a_samp>

#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 MAX_STRING (128)
   
stock bstrdel(string[], start, end, size = sizeof string)
{
    static buffer[MAX_STRING];
    strmid(buffer,string,0,(start-1),size);
    strmid(string,string,end,size,size);
    strins(string,buffer,0,size);
    buffer[0] = EOS;
}

static buffer2[MAX_STRING];
static buffer3[MAX_STRING];

stock bstrdel2(string[], start, end, size = sizeof string)
{
    strmid(buffer2, string, end, size, size);
    string[start] = EOS;
    strins(string, buffer2, start-1, end);
}

stock bstrdel3(string[], start, end, size = sizeof string)
{
    strmid(buffer3, string, (end + 1), size, size);
    string[start] = EOS;
    strcat(string,buffer3,size);
}

stock bstrdel4(string[], start, end)
{

    while(start != end)
    {
        string[start] = EOS;
        start++;
    }
}

main()
{
    new
        str1[] = "hello my world",
        str2[] = "hello my world",
        str3[] = "hello my world",
        str4[] = "hello my world",
        str5[] = "hello my world"
    ;
   
    START_BENCH(1000);
    {
        strdel(str1, 6, 8);
    }
    FINISH_BENCH("sa-mp");
   
    START_BENCH(1000);
    {
        bstrdel(str2, 6, 8);
    }
    FINISH_BENCH("b1");
   
    START_BENCH(1000);
    {
        bstrdel2(str3, 6, 8);
    }
    FINISH_BENCH("b2");
   
    START_BENCH(1000);
    {
        bstrdel3(str4, 6, 8);
    }
    FINISH_BENCH("b3");
   
    START_BENCH(1000);
    {
        bstrdel4(str5, 6, 8);
    }
    FINISH_BENCH("b4");
   
    //strdel(str1, 6, 8);
    printf("SAMP: %s",str1);
    //
    //bstrdel(str2, 6, 8);
    printf("b1: %s",str2);
    //
    //bstrdel2(str3, 6, 8);
    printf("b2: %s",str3);
    //
    //bstrdel3(str4, 6, 8);
    printf("b3: %s",str4);
   
    //bstrdel4(str5, 6, 8);
    printf("b5: %s",str4);
   
   
}
Bruno quando vocк executar o cуdigo no print vir isto:

Код:
[22:39:05] SAMP: hello 
[22:39:05] b1: hello
[22:39:05] b2: hello 
[22:39:05] b3: hello 
[22:39:05] b5: hello
Nгo sei porque acontece mas sei que й um bug por que isso acontece com o strdel e tudo xD
Se quiser verificar o funcionamento sу colocar a benchmark dentro dos /* */ e tirar os outros // , sу falta-me mais 100ms vou tentar ver como posso fazer de forma mais rбpida

Sobre o strins ainda nгo fiz mas tentarei tambйm
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)