[gnumeric] solver: make solver object keep track of elapsed time.



commit 8dda2eba3368283a3362106f45793fd0d5713a72
Author: Morten Welinder <terra gnome org>
Date:   Wed Jun 2 14:17:32 2010 -0400

    solver: make solver object keep track of elapsed time.

 src/dialogs/ChangeLog       |    4 ++
 src/dialogs/dialog-solver.c |    5 +--
 src/tools/ChangeLog         |    6 ++++
 src/tools/gnm-solver.c      |   66 ++++++++++++++++++++++++++++++++++++++++++-
 src/tools/gnm-solver.h      |    3 ++
 5 files changed, 80 insertions(+), 4 deletions(-)
---
diff --git a/src/dialogs/ChangeLog b/src/dialogs/ChangeLog
index 0d714d6..6367698 100644
--- a/src/dialogs/ChangeLog
+++ b/src/dialogs/ChangeLog
@@ -1,3 +1,7 @@
+2010-06-02  Morten Welinder  <terra gnome org>
+
+	* dialog-solver.c (cb_timer_tick): Use gnm_solver_elapsed.
+
 2010-05-31 Andreas J. Guelzow <aguelzow pyrshep ca>
 
 	* tool-dialogs.h (tool_setup_update): new
diff --git a/src/dialogs/dialog-solver.c b/src/dialogs/dialog-solver.c
index 80d4d9a..a129467 100644
--- a/src/dialogs/dialog-solver.c
+++ b/src/dialogs/dialog-solver.c
@@ -81,7 +81,6 @@ typedef struct {
 		GtkDialog   *dialog;
 		GtkWidget   *timer_widget;
 		guint       timer_source;
-		time_t      timer_start;
 		GtkWidget   *status_widget;
 		GtkWidget   *problem_status_widget;
 		GtkWidget   *objective_value_widget;
@@ -597,7 +596,8 @@ cb_notify_result (SolverState *state)
 static gboolean
 cb_timer_tick (SolverState *state)
 {
-	int secs = time (NULL) - state->run.timer_start;
+	double dsecs = gnm_solver_elapsed (state->run.solver);
+	int secs = (int)CLAMP (dsecs, 0, INT_MAX);
 	int hh = secs / 3600;
 	int mm = secs / 60 % 60;
 	int ss = secs % 60;
@@ -723,7 +723,6 @@ run_solver (SolverState *state, GnmSolverParameters *param)
 	g_object_ref (state->run.status_widget);
 	state->run.timer_source = g_timeout_add_seconds
 		(1, (GSourceFunc)cb_timer_tick, state);
-	state->run.timer_start = time (NULL);
 	cb_timer_tick (state);
 
 	/* ---------------------------------------- */
diff --git a/src/tools/ChangeLog b/src/tools/ChangeLog
index 8fa0adf..aaa9a23 100644
--- a/src/tools/ChangeLog
+++ b/src/tools/ChangeLog
@@ -1,5 +1,11 @@
 2010-06-02  Morten Welinder  <terra gnome org>
 
+	* gnm-solver.h (GnmSolver): Add new starttime and endtime members.
+
+	* gnm-solver.c (gnm_solver_set_status): Set starttime and endtime
+	as we go to or from "running".
+	(gnm_solver_elapsed): New function.
+
 	* tabulate.c (tabulation_eval): Evaluate only what we need.
 	(do_tabulation): Restore old values of dependents.
 
diff --git a/src/tools/gnm-solver.c b/src/tools/gnm-solver.c
index dd711dc..7826493 100644
--- a/src/tools/gnm-solver.c
+++ b/src/tools/gnm-solver.c
@@ -745,7 +745,9 @@ enum {
 	SOL_PROP_0,
 	SOL_PROP_STATUS,
 	SOL_PROP_PARAMS,
-	SOL_PROP_RESULT
+	SOL_PROP_RESULT,
+	SOL_PROP_STARTTIME,
+	SOL_PROP_ENDTIME
 };
 
 static GObjectClass *gnm_solver_parent_class;
@@ -794,6 +796,14 @@ gnm_solver_get_property (GObject *object, guint property_id,
 		g_value_set_object (value, sol->result);
 		break;
 
+	case SOL_PROP_STARTTIME:
+		g_value_set_double (value, sol->starttime);
+		break;
+
+	case SOL_PROP_ENDTIME:
+		g_value_set_double (value, sol->endtime);
+		break;
+
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 		break;
@@ -821,6 +831,14 @@ gnm_solver_set_property (GObject *object, guint property_id,
 		sol->result = g_value_dup_object (value);
 		break;
 
+	case SOL_PROP_STARTTIME:
+		sol->starttime = g_value_get_double (value);
+		break;
+
+	case SOL_PROP_ENDTIME:
+		sol->endtime = g_value_get_double (value);
+		break;
+
 	default:
 		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
 		break;
@@ -871,6 +889,23 @@ gnm_solver_stop (GnmSolver *sol, GError **err)
 	return res;
 }
 
+double
+gnm_solver_elapsed (GnmSolver *solver)
+{
+	double endtime;
+
+	g_return_val_if_fail (GNM_IS_SOLVER (solver), 0);
+
+	if (solver->starttime < 0)
+		return 0;
+
+	endtime = (solver->endtime < 0)
+		? time (NULL)
+		: solver->endtime;
+
+	return endtime - solver->starttime;
+}
+
 void
 gnm_solver_store_result (GnmSolver *sol)
 {
@@ -931,11 +966,26 @@ gnm_solver_finished (GnmSolver *sol)
 void
 gnm_solver_set_status (GnmSolver *solver, GnmSolverStatus status)
 {
+	GnmSolverStatus old_status;
+
+	g_return_if_fail (GNM_IS_SOLVER (solver));
+
 	if (status == solver->status)
 		return;
 
+	old_status = solver->status;
 	solver->status = status;
 	g_object_notify (G_OBJECT (solver), "status");
+
+	if (status == GNM_SOLVER_STATUS_RUNNING)
+		g_object_set (G_OBJECT (solver),
+			      "starttime", (double)time (NULL),
+			      "endtime", (double)-1,
+			      NULL);
+	else if (old_status == GNM_SOLVER_STATUS_RUNNING)
+		g_object_set (G_OBJECT (solver),
+			      "endtime", (double)time (NULL),
+			      NULL);
 }
 
 gboolean
@@ -1152,6 +1202,20 @@ gnm_solver_class_init (GObjectClass *object_class)
 				      GSF_PARAM_STATIC |
 				      G_PARAM_READWRITE));
 
+	g_object_class_install_property (object_class, SOL_PROP_STARTTIME,
+		 g_param_spec_double ("starttime", _("Start Time"),
+				      _("Time the solver was started"),
+				      -1, 1e10, -1,
+				      GSF_PARAM_STATIC |
+				      G_PARAM_READWRITE));
+
+	g_object_class_install_property (object_class, SOL_PROP_ENDTIME,
+		 g_param_spec_double ("endtime", _("End Time"),
+				      _("Time the solver finished"),
+				      -1, 1e10, -1,
+				      GSF_PARAM_STATIC |
+				      G_PARAM_READWRITE));
+
 	solver_signals[SOL_SIG_PREPARE] =
 		g_signal_new ("prepare",
 			      G_OBJECT_CLASS_TYPE (object_class),
diff --git a/src/tools/gnm-solver.h b/src/tools/gnm-solver.h
index 978075b..179d1da 100644
--- a/src/tools/gnm-solver.h
+++ b/src/tools/gnm-solver.h
@@ -197,6 +197,7 @@ typedef struct {
 	GnmSolverStatus status;
 	GnmSolverParameters *params;
 	GnmSolverResult *result;
+	double starttime, endtime;
 } GnmSolver;
 
 typedef struct {
@@ -222,6 +223,8 @@ void gnm_solver_set_status (GnmSolver *solver, GnmSolverStatus status);
 
 void gnm_solver_store_result (GnmSolver *solver);
 
+double gnm_solver_elapsed (GnmSolver *solver);
+
 gboolean gnm_solver_finished (GnmSolver *solver);
 
 gboolean gnm_solver_has_solution (GnmSolver *solver);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]