Timing code
#1

Hello, I'm trying to time some code to see which is faster. When i do it this way it returns a negative number.. Is this supposed to happen?

pawn Код:
CMD:test1(playerid, params[])
{
    new time1 = GetTickCount();
    new id;
    if(sscanf(params,"i",id)) return 0;
    for(new i = 0; i < 10000; i++) SendClientMessage(playerid, -1, "the big brown fox jumped over a hippo and a cow.");
    new time2 = GetTickCount();
    printf("test1 took %d ms to process", time1 - time2);
    return 1;
}

CMD:test2(playerid, params[])
{
    new time1 = GetTickCount();
    if(sscanf(params,"i",params[0])) return 0;
    for(new i = 0; i < 10000; i++) SendClientMessage(playerid, -1, "the big brown fox jumped over a hippo and a cow.");
    new time2 = GetTickCount();
    printf("test2 took %d ms to process", time1 - time2);
    return 1;
}
Reply
#2

You should be doing time2 - time1 like so:

pawn Код:
CMD:test1(playerid, params[])
{
    new time1 = GetTickCount();
    new id;
    if(sscanf(params,"i",id)) return 0;
    for(new i = 0; i < 10000; i++) SendClientMessage(playerid, -1, "the big brown fox jumped over a hippo and a cow.");
    new time2 = GetTickCount();
    printf("test1 took %d ms to process", time2 - time1);
    return 1;
}

CMD:test2(playerid, params[])
{
    new time1 = GetTickCount();
    if(sscanf(params,"i",params[0])) return 0;
    for(new i = 0; i < 10000; i++) SendClientMessage(playerid, -1, "the big brown fox jumped over a hippo and a cow.");
    new time2 = GetTickCount();
    printf("test2 took %d ms to process", time2 - time1);
    return 1;
}
Reply
#3

time1=12345;
time2=12346;
12345-12346=-1
yes
Код:
time2 - time1


edit: aaaw next time i will post before searching a proper picture ^^
edit2: LOL @ "the big brown fox jumped over a hippo and a cow". thats a worthy one!
edit3: grr cant the rep button be at the name, not at the posts end? lol
Reply
#4

Thanks :3
Reply
#5

Quote:
Originally Posted by Babul
Посмотреть сообщение
time1=12345;
time2=12346;
12345-12346=-1
yes
Код:
time2 - time1


edit: aaaw next time i will post before searching a proper picture ^^
hehe

edit: that's strange I would of thought just using the params would make the command faster because you're not making more variables.

Код:
[22:12:29] test1 took 25 ms to process
[22:12:35] test1 took 26 ms to process
[22:12:40] test1 took 21 ms to process
[22:12:47] test2 took 29 ms to process
[22:12:54] test2 took 29 ms to process
[22:12:58] test2 took 26 ms to process
Reply
#6

Variables don't really use much CPU or time to execute. You're not gaining any performance by using params[] directly.

Although, for only one-parameter commands you don't need sscanf, unless you really need some sscanf feature.
Reply
#7

Mean is correct. Doing this:

pawn Код:
if(sscanf(params,"i",id)) return 0;
Is the same as doing this:

pawn Код:
if(IsNumeric(params)) return 0;
In which case, you could just make a new variable (in this case, id), and make params a numerical value.

pawn Код:
new id = strval(params);
Also, when doing the time-tests, you don't need to create a second variable for GetTickCount(). You can just use GetTickCount() inside of the print function, like so:

pawn Код:
printf("Execution took %dms.", GetTickCount()-iStart);
Reply
#8

Thanks for the information guys
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)