我刚刚用 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/