1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
24 typedef struct _GRealTimer GRealTimer;
39 timer = g_new (GRealTimer, 1);
42 gettimeofday (&timer->start, NULL);
44 return ((GTimer*) timer);
48 g_timer_destroy (GTimer *timer)
50 g_assert (timer != NULL);
56 g_timer_start (GTimer *timer)
60 g_assert (timer != NULL);
62 rtimer = (GRealTimer*) timer;
63 gettimeofday (&rtimer->start, NULL);
68 g_timer_stop (GTimer *timer)
72 g_assert (timer != NULL);
74 rtimer = (GRealTimer*) timer;
75 gettimeofday (&rtimer->end, NULL);
80 g_timer_reset (GTimer *timer)
84 g_assert (timer != NULL);
86 rtimer = (GRealTimer*) timer;
87 gettimeofday (&rtimer->start, NULL);
91 g_timer_elapsed (GTimer *timer,
95 struct timeval elapsed;
98 g_assert (timer != NULL);
100 rtimer = (GRealTimer*) timer;
103 gettimeofday (&rtimer->end, NULL);
105 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
107 rtimer->end.tv_usec += 1000000;
108 rtimer->end.tv_sec--;
111 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
112 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
114 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
117 *microseconds = elapsed.tv_usec;