clock_gettime:在 Windows 10 的 Visual Studio 中找不到标识符
clock_gettime: identifier not found in Visual Studio in Windows 10
>我尝试运行这个程序,在Visual Studio 2015中借助clock_gettime来执行功能所需的时间。 我遵循了这里的参考:https://www.cs.rutgers.edu/~pxk/416/notes/c-tutorials/gettime.html
#include <iostream>
#include <stdio.h> /* for printf */
#include <stdint.h> /* for uint64 definition */
#include <stdlib.h> /* for exit() definition */
#include <ctime>
#include<windows.h>
#define _POSIX_C_SOURCE 200809L
#define BILLION 1000000000L
void fun() {
Sleep(3);
}
int main()
{
struct timespec start, end;
int i;
uint64_t diff;
/* measure monotonic time */
clock_gettime(CLOCK_MONOTONIC, &start); /* mark start time */
fun();
clock_gettime(CLOCK_MONOTONIC, &end); /* mark the end time */
diff = BILLION * (end.tv_sec - start.tv_sec) + end.tv_nsec - start.tv_nsec;
printf("elapsed time = %llu nanosecondsn", (long long unsigned int) diff);
system("pause");
return 0;
}
我尝试在 Linux 中运行,它工作正常。但在Windows中,VS 2015显示错误。
'CLOCK_MONOTONIC' : undeclared identifier
'clock_gettime': identifier not found
请建议我如何修复此错误或如何在Visual Studio 2015中查找经过的时间。谢谢。
函数clock_gettime((由POSIX定义。Windows不符合POSIX标准。
这是将clock_gettime((移植到Windows的旧帖子的链接。
对于Windows,我会使用std::chrono库。
简单的例子是:
#include <chrono>
auto start = std::chrono::high_resolution_clock::now();
func();
auto end = std::chrono::high_resolution_clock::now();
std::chrono::duration<float> duration = end - start;
printf("Duration : %f", duration.count());
相关文章:
- Visual Studio 2015:Extern "C" 和 "export" 关键字
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 试图在visual studio上用C++创建一个桌面应用程序
- 如何在MS Visual Studio 2019中运行QT UI
- 如何使用Visual Studio 2017在C++中为参数化对象数组使用唯一指针
- Visual Studio Code - C++ Debugger 無法啟動
- Visual Studio 2019:插入多个C++风格的单行注释
- Visual Studio Code "undefined reference to `WinMain@16'"
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- Visual Studio中的函数声明和函数定义问题
- 如何指定我希望我的LIB链接到的DLL文件?-Visual Studio 2019
- Visual Studio mkl_link_tool.exe链接错误
- Visual Studio(或任何其他工具)能否将地址解释为调用堆栈(boost上下文)的开头
- 不同的Visual Studio版本中缺少.dll
- 用Visual Studio在C++中嵌入Julia
- Visual Studio 2017循环自动向量化问题
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- 尝试使用继承和模板实现CRTP.Visual Studio正在生成编译器错误
- Visual Studio在尝试读取resource.txt文件时崩溃
- Visual Studio 2017 不允许我创建 C++ 专用模板