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 Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 typedef struct _GRealTimer GRealTimer;
38 timer = g_new (GRealTimer, 1);
41 gettimeofday (&timer->start, NULL);
43 return ((GTimer*) timer);
47 g_timer_destroy (GTimer *timer)
49 g_assert (timer != NULL);
55 g_timer_start (GTimer *timer)
59 g_assert (timer != NULL);
61 rtimer = (GRealTimer*) timer;
62 gettimeofday (&rtimer->start, NULL);
67 g_timer_stop (GTimer *timer)
71 g_assert (timer != NULL);
73 rtimer = (GRealTimer*) timer;
74 gettimeofday (&rtimer->end, NULL);
79 g_timer_reset (GTimer *timer)
83 g_assert (timer != NULL);
85 rtimer = (GRealTimer*) timer;
86 gettimeofday (&rtimer->start, NULL);
90 g_timer_elapsed (GTimer *timer,
94 struct timeval elapsed;
97 g_assert (timer != NULL);
99 rtimer = (GRealTimer*) timer;
102 gettimeofday (&rtimer->end, NULL);
104 if (rtimer->start.tv_usec > rtimer->end.tv_usec)
106 rtimer->end.tv_usec += 1000000;
107 rtimer->end.tv_sec--;
110 elapsed.tv_usec = rtimer->end.tv_usec - rtimer->start.tv_usec;
111 elapsed.tv_sec = rtimer->end.tv_sec - rtimer->start.tv_sec;
113 total = elapsed.tv_sec + ((gdouble) elapsed.tv_usec / 1e6);
116 *microseconds = elapsed.tv_usec;