在编译时发现可用的时钟类型
Discover available clock types at compile time
[Ubuntu 14.04、3.16.0-34 通用内核、GCC 4.8.4、Clang 3.5.0]
我正在为我的应用程序编写一些经过时间的性能例程,我想以跨平台的方式进行。
我想以这样一种方式编写它,即在编译期间选择正确的时钟类型,而不是 运行-time(我可以通过测试故障和使用回退来做到这一点)。
clock_getres(2)
手册页指出:
On POSIX systems on which these functions are available, the symbol _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0. The symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
indicate that CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID are available. (See also sysconf(3).)
我在我的代码中包含了 , , 但是我的条件编译语句无法识别符号 _POSIX_MONOTONIC_CLOCK
。它总是打印 'gettimeofday' 消息。我试过 GCC 和 Clang,但得到了相同的结果。
我下面的代码不完整(而且不正确),如果能提供一些正确的帮助,我将不胜感激。
#include <unistd.h>
#include <features.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}
我没有使用任何配置或自动配置软件。
此外,关于 CLOCK_MONOTONIC 和 CLOCK_MONOTONIC_RAW 的相对速度的评论会很好。我了解其中的区别及其局限性。
您没有声明 man
页面上说您需要的 headers 和 feature-test 宏。这有效:
#define _POSIX_C_SOURCE 200809L // <- This was missing.
#define _XOPEN_SOURCE 700 // <- This is optional.
#include <unistd.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
#include <time.h> // <- This was missing.
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}
[Ubuntu 14.04、3.16.0-34 通用内核、GCC 4.8.4、Clang 3.5.0]
我正在为我的应用程序编写一些经过时间的性能例程,我想以跨平台的方式进行。
我想以这样一种方式编写它,即在编译期间选择正确的时钟类型,而不是 运行-time(我可以通过测试故障和使用回退来做到这一点)。
clock_getres(2)
手册页指出:
On POSIX systems on which these functions are available, the symbol _POSIX_TIMERS is defined in <unistd.h> to a value greater than 0. The symbols _POSIX_MONOTONIC_CLOCK, _POSIX_CPUTIME, _POSIX_THREAD_CPUTIME
indicate that CLOCK_MONOTONIC, CLOCK_PROCESS_CPUTIME_ID, CLOCK_THREAD_CPUTIME_ID are available. (See also sysconf(3).)
我在我的代码中包含了 , , 但是我的条件编译语句无法识别符号 _POSIX_MONOTONIC_CLOCK
。它总是打印 'gettimeofday' 消息。我试过 GCC 和 Clang,但得到了相同的结果。
我下面的代码不完整(而且不正确),如果能提供一些正确的帮助,我将不胜感激。
#include <unistd.h>
#include <features.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}
我没有使用任何配置或自动配置软件。
此外,关于 CLOCK_MONOTONIC 和 CLOCK_MONOTONIC_RAW 的相对速度的评论会很好。我了解其中的区别及其局限性。
您没有声明 man
页面上说您需要的 headers 和 feature-test 宏。这有效:
#define _POSIX_C_SOURCE 200809L // <- This was missing.
#define _XOPEN_SOURCE 700 // <- This is optional.
#include <unistd.h>
#include <stdio.h> // printf (otherwise forward declaration warning)
#include <stdlib.h> // NULL
#include <sys/time.h>
#include <time.h> // <- This was missing.
long long get_utime(void)
{
struct timeval tv;
if (gettimeofday(&tv, NULL) == -1) return -1LL;
long long t = (long long) tv.tv_usec +
(long long) tv.tv_sec * 1000000LL;
return t;
}
int main() {
union time{
struct timespec tSpec;
long long tLL;
} myT;
#ifdef CLOCK_MONOTONIC_RAW
clock_gettime(CLOCK_MONOTONIC_RAW, &myT.tSpec);
printf("CLOCK_MONOTONIC_RAW\n");
#elif _POSIX_MONOTONIC_CLOCK
clock_gettime(CLOCK_MONOTONIC, &myT.tSpec);
printf("CLOCK_MONOTONIC\n");
#else
myT.tLL = get_utime();
printf("gettimeofday\n");
#endif // CLOCK_MONOTONIC_RAW
return 0;
}