-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathccpal.h
94 lines (72 loc) · 2.79 KB
/
ccpal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Name: CCPal = C Code Performance Analysis Library
* Purpose: Measure performance (in terms of execution time) of
* C routines (useful to measure perfs as Unit Test)
*
* Author: Paolo Fabio Zaino
* License: Copyright by Paolo Fabio Zaino, all rights reserved
* Distributed under GPL v2 license
*
* Description:
* CCPal is a SIMPLE C Code runtime Performance Analysis Library
* This simple library allow the user to measure code execution
* time and works on a number of Operating Systems natively, so
* you just need to add it to your project and use it.
*
* At the moment this library seems to be working well on ALL
* tested Intel based Linux Distro (RHEL, CentOS, PacketLinux,
* ScientificLinux, Debian, Ubuntu, KALI) and all modern releases
* of Apple MacOS.
*/
#ifndef CCPAL_H_
#define CCPAL_H_
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
#pragma once
#endif
#include <time.h>
#include <sys/time.h>
#ifdef __MACH__
#include <mach/clock.h>
#include <mach/mach.h>
#endif
#ifdef __APPLE__
#define CCPAL_INIT_LIB struct timespec tsi, tsf; \
double elaps_s; long elaps_ns; \
clock_serv_t cclock; \
mach_timespec_t mts;
#else
#define CCPAL_INIT_LIB struct timespec tsi, tsf; \
double elaps_s; long elaps_ns;
#endif
#ifdef __APPLE__
#define CCPAL_START_MEASURING \
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); \
clock_get_time(cclock, &mts); \
mach_port_deallocate(mach_task_self(), cclock); \
tsi.tv_sec = mts.tv_sec; \
tsi.tv_nsec = mts.tv_nsec;
#define CCPAL_STOP_MEASURING \
host_get_clock_service(mach_host_self(), CALENDAR_CLOCK, &cclock); \
clock_get_time(cclock, &mts); \
mach_port_deallocate(mach_task_self(), cclock); \
tsf.tv_sec = mts.tv_sec; \
tsf.tv_nsec = mts.tv_nsec; \
elaps_s = difftime(tsf.tv_sec, tsi.tv_sec); \
elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
#else
#ifdef CLOCK_PROCESS_CPUTIME_ID
/* cpu time in the current process */
#define CCPAL_START_MEASURING clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tsi);
#define CCPAL_STOP_MEASURING clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &tsf); \
elaps_s = difftime(tsf.tv_sec, tsi.tv_sec); \
elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
#else
/* this one should be appropriate to avoid errors on multiprocessors systems */
#define CCPAL_START_MEASURING clock_gettime(CLOCK_MONOTONIC_RAW, &tsi);
#define CCPAL_STOP_MEASURING clock_gettime(CLOCK_MONOTONIC_RAW, &tsf); \
elaps_s = difftime(tsf.tv_sec, tsi.tv_sec); \
elaps_ns = tsf.tv_nsec - tsi.tv_nsec;
#endif
#endif
#define CCPAL_REPORT_ANALYSIS fprintf (stdout, "We have spent %lf seconds executing previous code section.\n", elaps_s + ((double)elaps_ns) / 1.0e9 );
#endif // CCPAL_H_