SA-MP Forums Archive
checking CPU usage - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: checking CPU usage (/showthread.php?tid=267298)



checking CPU usage - Edga - 07.07.2011

Is there a way to check exactly which part of your script uses a certain amount of CPU ?


Re: checking CPU usage - Donya - 07.07.2011

check ******'s post o.o


Re: checking CPU usage - Edga - 08.07.2011

Quote:
Originally Posted by ******
Посмотреть сообщение
Yes - it's called "profiling" and there's loads of work in general programming on this. Here is a nice generic macro for you, note that you will need additional function (or just one called "DoProfile(funcname[], time);") to make it work, this will also not currently work with functions taking arrays or strings as parameters (it is possible to make it work, but it's a VERY complex macro to do so (in fact many macros)):

pawn Код:
#define profile%0(%1) %0(%1){new _f=GetTickCount(),y=_:%0__(%1);DoProfile(#%0,GetTickCount()-_f);return y;}%0__(%1)
Then you can do:

pawn Код:
profile MyFunc(a, b)
{
    // Code here.
    return 0; // Must have a return.
}
Every time you call "MyFunc" the function "DoProfile" will be called with the name of the function and the amount of time the function was called for. I did consider building something like this in to YSI, but the remote call macros in there are already too complex to do it within the line length limits (I think - it may be worth investigating).

What you do in "DoProfile" is up to you (and note that you will get a LOT of "0"s because most of your functions (hopefully all of them) will run too fast to be detected). In fact, ignore that, the only real way to do this is to call functions many many times and average the call times, however that doesn't tell you if slow functions are called a lot, so really this can only be done with a plugin. In fact this would be a good extension to the crash detect plugin which already requires -d0 and recommends -r on the compiler to get function information and can get stack trace information. I smell a new useful plugin.
Right, i am a bit confused... So do i do this or what ?

if(strcmp(cmdtext, "/test", true)==0)
{
Function();
return 1;
}

profile Function()
{
// code
return 0;
}