Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / libfreerdp-utils / stopwatch.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Client
3  * Stopwatch Utils
4  *
5  * Copyright 2011 Stephen Erisman
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #include <freerdp/utils/stopwatch.h>
21
22 STOPWATCH* stopwatch_create()
23 {
24         STOPWATCH* sw;
25
26         sw = (STOPWATCH*) xmalloc(sizeof(STOPWATCH));
27         stopwatch_reset(sw);
28
29         return sw;
30 }
31
32 void stopwatch_free(STOPWATCH* stopwatch)
33 {
34         xfree(stopwatch);
35 }
36
37 void stopwatch_start(STOPWATCH* stopwatch)
38 {
39         stopwatch->start = clock();
40         stopwatch->count++;
41 }
42
43 void stopwatch_stop(STOPWATCH* stopwatch)
44 {
45         stopwatch->end = clock();
46         stopwatch->elapsed += (stopwatch->end - stopwatch->start);
47 }
48
49 void stopwatch_reset(STOPWATCH* stopwatch)
50 {
51         stopwatch->start = 0;
52         stopwatch->end = 0;
53         stopwatch->elapsed = 0;
54         stopwatch->count = 0;
55 }
56
57 double stopwatch_get_elapsed_time_in_seconds(STOPWATCH* stopwatch)
58 {
59         return ((double) stopwatch->elapsed) / CLOCKS_PER_SEC;
60 }
61
62 void stopwatch_get_elapsed_time_in_useconds(STOPWATCH* stopwatch, uint32* sec, uint32* usec)
63 {
64         double uelapsed;
65         double clocks_per_usec;
66
67         *sec = ((uint32) stopwatch->elapsed) / CLOCKS_PER_SEC;
68         uelapsed = stopwatch->elapsed - ((double)(*sec) * CLOCKS_PER_SEC);
69
70         clocks_per_usec = (CLOCKS_PER_SEC / 1000000);
71
72         if (clocks_per_usec > 0.0)
73                 *usec = (uint32)(uelapsed / clocks_per_usec);
74         else
75                 *usec = 0;
76 }
77