Open Source on IBM i

Measuring Time Spent

Sometimes you have a block of code and just want to know how long it took the machine to execute it.

The Java developers have some really cool metric libraries like the Dropwizard subproject Metrics.

But for the quick and dirty case there is always

System.currentTimeMillis();

For the RPG developer there are some built-in functions which can be used for this, but nothing is as straightforward as the Java solution.

But if we tap into the powers of ILE and use some C functions in RPG we can come very close to the Java solution.

The C function time() returns the Unix timestamp in an integer. The prototype looks like this.

dcl-pr time int(10) extproc('time');
  returnValue pointer;
end-pr;

The function also takes a parameter for output but we don't need this and can pass *NULL for it.

dcl-s NULL pointer;
dcl-s start int(10);

start = time(NULL);

dsply %trimr('Time spent: ' + %char(time(NULL) - start));

Note: The resolution of time() is seconds. If milliseconds are needed another function should be used.

Tags : RPG