2023-06-27 05:36:50 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
2023-07-13 06:03:28 +08:00
|
|
|
typedef long long hrtime_t;
|
|
|
|
|
|
|
|
hrtime_t
|
2023-06-27 05:36:50 +08:00
|
|
|
gethrtime (void)
|
|
|
|
{
|
|
|
|
struct timespec tp;
|
2023-07-13 06:03:28 +08:00
|
|
|
hrtime_t rc = 0;
|
2023-06-27 05:36:50 +08:00
|
|
|
#ifdef CLOCK_MONOTONIC_RAW
|
|
|
|
int r = clock_gettime (CLOCK_MONOTONIC_RAW, &tp);
|
|
|
|
#else
|
|
|
|
int r = clock_gettime (CLOCK_MONOTONIC, &tp);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (r == 0)
|
2023-07-13 06:03:28 +08:00
|
|
|
rc = ((hrtime_t) tp.tv_sec) * 1e9 + (hrtime_t) tp.tv_nsec;
|
2023-06-27 05:36:50 +08:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
volatile long x; /* temp variable for long calculation */
|
|
|
|
|
|
|
|
int
|
|
|
|
main (int argc, char **argv)
|
|
|
|
{
|
|
|
|
long long count = 0;
|
2023-07-13 06:03:28 +08:00
|
|
|
hrtime_t start = gethrtime ();
|
2023-06-27 05:36:50 +08:00
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
for (int j = 0; j < 1000000; j++)
|
|
|
|
x = x + 1;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
while (start + 2e9 > gethrtime ());
|
|
|
|
printf("count=%lld x=%lld\n", count, x);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|