IBM Support

Obtain the elapsed CPU time for CPLEX application

Question & Answer


Question

How do I obtain the elapsed CPU time for my CPLEX application?

Answer

As of CPLEX 12.1, there is no direct API in the C interface for getting elapsed time; you would need to use system functions here. For example:

#include <time.h>
#include <ilcplex/cplex.h>
int
main(int argc, char **argv)
{
time_t start, end;
/* returns elapsed time in sec */
// clock_t start, end;
/* for elapsed CPU time */
double total_time;
start = clock();
/* CPLEX routines here */
...
end = clock();
total_time = (double)( end - start )/(double)CLK_TCK ;
printf( "\nElapsed time : %0.3f \n", total_time );
return 0;
}

In the Concert interface (C++), you can use the getTime() function of IloAlgorithm (a parent class of IloCplex) to get the current time since the most recent reset of the invoking algorithm. You can calculate the difference between two getTime() calls to identify the run time of an optimization. However, there is no Concert method available for Java to do the same. You need to use either System.currentTimeMillis() or use the Date.getTime() to achieve this.

In either case, the type of time returned is platform dependent. On Windows systems, the time returned is elapsed wall clock time. On UNIX systems, the time returned is CPU time.

Following is the sample code for Java (Windows) to calculate the time taken to solve the problem:

Using Date.getTime():

Date start = new Date();
cplex.solve();
Date end = new Date();
System.out.println(" Problem tackled in " + 
(end.getTime()-start.getTime())/1000. + " seconds");

Using System.currentTimeMillis():

long start = System.currentTimeMillis();
cplex.solve();
long end = System.currentTimeMillis();
System.out.println(" Problem tackled in " + (end - start)/1000. + " seconds 
using currentTimeMillis");

[{"Product":{"code":"SSSA5P","label":"IBM ILOG CPLEX Optimization Studio"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Not Applicable","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF016","label":"Linux"},{"code":"PF014","label":"iOS"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"12.6;12.5;12.4;12.3;12.2","Edition":"","Line of Business":{"code":"LOB10","label":"Data and AI"}}]

Historical Number

cplex/FAQ/64

Document Information

Modified date:
16 June 2018

UID

swg21399982