3
$\begingroup$

I am solving a MILP model in C++ using Gurobi 7.5.2. More specifically, I am determining the CPU time of my C++ program via the following commands:

  • clock_t start_time;
  • double elapsed_time;
  • start_time= clock();
  • model.optimize();
  • elapsed_time = (double) (clock() - start_time)/CLOCKS_PER_SEC;

When I compare the elapsed_time with the Gurobi runtime, the Gurobi runtime does show the actual duration (measured by a chronometer) but the calculated elapsed_time of C++ is way higher. Why is the elapsed_time not the same as the Gurobi runtime and the actual duration (measured by the chronometer)?

$\endgroup$
1
$\begingroup$

This is more of a C++ question, so you should rather look for similar questions on stackoverflow.

A common issue is that clock() is not accurate for short durations, so you may want one of the high resolution C++ timers instead, as shown in an answer here.

EDIT: as commented by user3680510 and Stradivari, this is due to the use of multithreading, as clock() measures the CPU time. The C++ timer above (or C's time()) will behave correctly.

$\endgroup$
1
$\begingroup$

clock() measures CPU time instead of real time. In order to measure real time in C++ use:

auto start = std::chrono::steady_clock::now();

//do stuff

auto end = std::chrono::steady_clock::now();

auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.