[ Bristol CS | Index | Tom Hinton ]

Measuring CPU time used in Java or Matlab

If you want do empirical complexity measurements when running programs on the ML cluster, you really need to measure time on the CPU rather than wall-clock time. Here are some bits to connect your java or matlab code with the getrusage (2) system call.

Using the Java Native Interface

The Java part of the interface is in CPUTime.java. It provides a CPUTime object which when constructed captures the user & system time used by the java process at construction time. To measure elapsed time do something like:

  CPUTime time = new CPUTime();
  doSomethingDifficult();
  CPUTime time2 = new CPUTime();
  
  double secondsElapsed = time2.getTotalTime() - time.getTotalTime();

The native part is in CPUTime.cxx, and needs to be compiled to a shared library by doing something like:

  $ g++ -o libcputime.so -shared CPUTime.cxx

Then when you invoke Java you need to have libcputime.so in your library path, which you could do like this (if libcputime.so is in the current directory):

  $ LD_LIBRARY_PATH=`pwd` java Something

Using MEX

The Matlab interface is in get_cputime.c. To get Matlab to see it you need to use mex on it once, by doing something like

  mex get_cputime.c

at the beginning of your script/session. This will define the matlab function get_cputime() which returns a two-columned vector containing user time in the first column and system time in the second, so you could measure total time with sum(get_cputime()).

Valid XHTML 1.1! Valid CSS!