# Why does the getTime() function from cplex concert returns wrong value?

I am running a MILP formulation (implemented in C++) with the Cplex Concert Technology 12.10, and I am trying to get the total elapsed time. So till the moment, I have tried three approaches: Be cplex my IloCplex object.

1. Using the clock() function from the time.h C++ library:
    cplex.setParam(IloCplex::Param::TimeLimit, time_limit);
time_t start = clock();
cplex.solve();
double total_time = (double) (clock() - start) / (double) CLOCKS_PER_SEC;

1. Using the Cplex Concert function getTime():
    cplex.setParam(IloCplex::Param::TimeLimit, time_limit);
cplex.solve();
double total_time = cplex.getTime();

1. Using the Cplex Concert parameter ClockType:
    cplex.setParam(IloCplex::Param::TimeLimit, time_limit);
cplex.setParam(IloCplex::Param::ClockType, 2);
cplex.solve();
double total_time = cplex.getTime();


My code does not make use of any callbacks (informational callback neither), and does not set any value to the Cplex Concert parameter Threads, i.e., the piece of code cplex.setParam(IloCplex::Param::Threads, n); is not executed. Since such piece of code is not executed, then (according to the Cplex Concert official documentation) my code is using all available threads:

When this parameter is at its default setting 0 (zero), and your application includes no callbacks or only an informational callback, CPLEX can use all available threads.

The problem that I am facing is that in all these three approaches the total_time presents a wrong value. For example, if I set the variable time_limit to 120, i.e., set the optimization time limit to 120 seconds, then the variable total_time presents a value much bigger than the expected one, such as 892.322.

So, I'd like to know if anyone already faced this problem before.

A solution can be found here c-using-clock-to-measure-time-in-multi-threaded-programs.

Since you do not limit the threads, Cplex will span as many as there are cores. You are counting the cpu time (and since each core contributes all of its running time) you see a much larger value. I think you are interested in the wall-time of the process.

$$892/120 \approx 7.4$$. So i guess you have 8 cores (or 4 with hyperthreading). When you limit the threads to 1 you should see a value of about 120.

• Very thank you. May 24 '20 at 2:56