After the optimization problem is solved, the solution time will be displayed in the command window, shown as follows:

You could record the solution time manually, however, there is a method that you could save the solution time into a .txt file along with the values of objective and variables so that you could check back whenever you need to.
- Please include a head file:
#include <ctime>
#include <fstream>
- Define the time stamps at the beginning of the code:
time_t T_start, T_data_proc, T_Gurobi_proc, T_sol;
I defined 4 of them, because I want to know how long it takes to read and process the system data, how long it takes for Gurobi to formulate all the variables and constraints, and how long it takes to solve the optimization problem. In this case:
- The first one should be placed right after defining these variables.
T_start = time(0);
- The second one should be placed after you put all your test system data into the format that you could use:
T_data_proc = time(0);
- The third one should be placed right before
MyModel.optimize();
T_Gurobi_proc = time(0);
- The fourth one should be placed right after
MyModel.optimize();
T_sol = time(0);
- Calculate the time and save it to a .txt file:
ofstream results;
results.open ("results.txt");
results << "Time to process input data: " << difftime(T_data_proc, T_start) <<" second(s)."<< endl;
results << "Time to formulate the problem in Gurobi: " << difftime(T_Gurobi_proc, T_data_proc) <<" second(s)."<< endl;
results << "Time to solve the problem in Gurobi: " << difftime(T_sol, T_Gurobi_proc) <<" second(s)."<< endl;
results << "Total Time to run the code: " << difftime(T_sol, T_start) <<" second(s)."<< endl;
results.close();