Wednesday, April 10, 2013

How to: Time how long AX code takes to run in X++ (Performance metrics)

If you need to time some code to see how long something ran and you don't want to use the code profiler, you can do the below using the WinAPIServer::getTickCount();

Pretty simple...

int ticksBegin;
int ticksEnd;
int totalTicks;

int seconds;
int minutes;
int hours;

// Grab the current 'tick' count in AX
ticksBegin = WinAPIServer::getTickCount();
ticksEnd = WinAPIServer::getTickCount(); 
totalTicks = ticksEnd - ticksBegin;    

// Find the seconds
seconds = totalTicks/1000;
info (strFmt("%1 seconds", seconds));

// Find the minutes
minutes = seconds/60;
info (strFmt("%1 minutes", minutes));

// Find the hours
hours = minutes/60;
info (strFmt("%1 hours", hours));

// This will show the time in hh:mm:ss format except there may be only one value (e.g. hh:m:ss)
info (strFmt("%1:%2:%3", hours, minutes mod 60, seconds mod 60)); 

No comments:

Post a Comment