How to mark the time when a function is finished (for checking speed)
#1

I want to make something that gets the time of when the function is finished( for eg. a command), like for checking the speed of the function but i don't get it. Please show me how to do it except from giving links, thank you.
Reply
#2

pawn Код:
forward Function();

new Timer;
new FunctionTimer;
pawn Код:
public OnGameModeInit()
{
Timer = SetTimer("Function", 1000, true);
}
pawn Код:
public Function()
{
//Do your function here...
FunctionTimer++;
//If(statement == something) {
KillTimer(Timer)
new message[48];
format(message,sizeof(message), "It took %i seconds.", FunctionTimer);
SendClientMessageToAll(COLOR_HERE, message);
}
Reply
#3

Thats not working please somebody help me fast.
Reply
#4

Quote:
Originally Posted by Conroy
pawn Код:
forward Function();

new Timer;
new FunctionTimer;
pawn Код:
public OnGameModeInit()
{
Timer = SetTimer("Function", 1000, true);
}
pawn Код:
public Function()
{
//Do your function here...
FunctionTimer++;
//If(statement == something) {
KillTimer(Timer)
new message[48];
format(message,sizeof(message), "It took %i seconds.", FunctionTimer);
SendClientMessageToAll(COLOR_HERE, message);
}
Worst possible way of doing that lol. If one of my functions took a second to execute, i'd probably hang myself lol. Even if you wanted to measure something in seconds, you should NEVER do it like that. You can multiply the GetTickCount result by 1000, or even use gettime(); instead of GetTickCount, that would also give you the time in seconds.

Use GetTickCount, here's a very simple example.

pawn Код:
new Tick = GetTickCount(), Tick2;
    YouFunctionToTest();
    Tick2 = GetTickCount();
   
    printf("***** Time took in ms: %d", Tick2-Tick);
Reply
#5

I would be realy great full if you explained this too me a little bit because i keep getting 0 on console.
Reply
#6

I am using it on a command, to check the speed of the command.
Reply
#7

That's probably correct if it's 0, it's because your function takes < 1ms (which can be expected for fast code). If you want to measure how many nano seconds it is, then loop the function 1000000 times (do note that this time will also include the loop execution time).. but it's the closest value you are going to get to nanoseconds.

pawn Код:
new
  startTick = GetTickCount(); // Get the start ticks (in ms)

for(new i = 0; i < 1000000; i++)
{
  TheFunction();
}

printf("TheFunction() took ~%dns to complete.", GetTickCount() - startTick); // Print the difference between ticks (1 tick each ms)
Reply
#8

Yeah, i probably should have done that from the begging, but i was in the middle of something.


GetTickCount returns how long your server has been up in milliseconds. 1000 milliseconds is equal to 1 second, so you can see how using ms is better than seconds for benchmarking :P.

The return value of GetTickCount is normally very high number, such as: 550861941. subtracting a new tick from the tick you stored before your function/command will give your time:

550861946 - 550861941 = 5.

So, newtick-oldtick = time_in_ms. I cant think of any other way to explain it so, hope you get it now aha.

Reply
#9

Thankyou very much guys.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)