2 Simple timer for the Midnight Commander.
4 Copyright (C) 2013-2017
5 Free Software Foundation, Inc.
10 This file is part of the Midnight Commander.
12 The Midnight Commander is free software: you can redistribute it
13 and/or modify it under the terms of the GNU General Public License as
14 published by the Free Software Foundation, either version 3 of the License,
15 or (at your option) any later version.
17 The Midnight Commander is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 * \brief Source: simple timer
34 #include "lib/global.h"
35 #include "lib/timer.h"
37 /*** global variables ****************************************************************************/
42 * Opaque datatype that records a start time.
43 * #mc_timer_t records a start time, and counts microseconds elapsed since
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60 /*** public functions ****************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
64 * Creates a new timer, and starts timing.
66 * @return: a new #mc_timer_t.
74 timer
= g_new (mc_timer_t
, 1);
75 gettimeofday (&tv
, NULL
);
76 timer
->start
= (guint64
) tv
.tv_sec
* G_USEC_PER_SEC
+ (guint64
) tv
.tv_usec
;
81 /* --------------------------------------------------------------------------------------------- */
84 * Destroys a timer, freeing associated resources.
86 * @timer: an #mc_timer_t to destroy.
89 mc_timer_destroy (mc_timer_t
* timer
)
94 /* --------------------------------------------------------------------------------------------- */
97 * Obtains the time since the timer was started.
99 * @timer: an #mc_timer_t.
101 * @return: microseconds elapsed, the time since the timer was started
105 mc_timer_elapsed (const mc_timer_t
* timer
)
109 gettimeofday (&tv
, NULL
);
111 return ((guint64
) tv
.tv_sec
* G_USEC_PER_SEC
+ (guint64
) tv
.tv_usec
- timer
->start
);
114 /* --------------------------------------------------------------------------------------------- */