gpt4 book ai didi

C++ 与 System.DateTime.Now.Ticks 等效的东西?

转载 作者:行者123 更新时间:2023-11-28 03:20:01 25 4
gpt4 key购买 nike

我只是用 C# 开发游戏,我刚迁移到 C++,我只想将游戏重写成 C++,几乎成功地将所有代码从 C# 转换为 C++,但是有一些问题,我的游戏在 C# 中使用 System.DateTime.Now.Ticks...我被卡住了

所以..在 C++ 中有与 System.DateTime.Now.Ticks 等效的东西吗?

最佳答案

看看这个 tutorial on cplusplus.com独立于平台。

函数Clock()返回此进程已消耗的抽动量。要访问此功能,您需要包括:#include <time.h> .

要在几秒钟内得到同样的东西,你可以使用 CLOCKS_PER_SEC宏,所以你可以这样做:Clock() / CLOCKS_PER_SEC获取此过程花费的秒数。但请记住,您可能会以这种方式失去一些精度。


虽然你可能不需要这个确切的功能......如果你正在寻找自程序启动以来经过的确切时间,你(据我所知)必须使用 difftime() 功能。如果您需要精确的控制,您可能会失去一些精度,具体取决于您的平台。

这样你必须在申请开始时保存当前时间,并在申请期间从当前时间中减去它。

#include <stdio.h>
#include <time.h>

time_t programstart;

int main ()
{
time(&programstart); //assign the current time

/*... the program ...*/

//get seconds since start
double secondssincestart = difftime(time(NULL), programstart);

printf ("the program took: %f seconds\n", secondssincestart);

return 0;
}

编辑:

由于这篇文章仍然受到一些关注,重要的是要注意,今天的 C++11 具有标准、易于使用且非常方便的库 chrono .

关于C++ 与 System.DateTime.Now.Ticks 等效的东西?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15726284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com