【问题标题】:To display the time in Xcode在 Xcode 中显示时间
【发布时间】:2021-01-09 01:57:50
【问题描述】:

我在 Xcode 中用 C 语言创建了一个简单的游戏,但是时间没有流逝。有谁知道如何解决这个问题?

int main(void)
{
   long startTime = 0;
   long totalTime = 0;
   long prevtime = 0;
   int num;
   init();
   cursor = arrayFish;
   startTime = clock();
   while (1)
   {
     printFishes();
     printf("\n Which fishing port would you like to water? ");
     scanf("%d", &num);
     printf("\n");
     if (num > 6 || num < 1)
     {
       printf("Please re-enter.\n");
       continue;
     }
   }
   totalTime = (clock() - startTime) / CLOCKS_PER_SEC;
   printf("Total Elapsed Time: %ld\n", totalTime);
}
 

除了时间标记外它可以工作。所以我只写了需要的代码。谢谢。

【问题讨论】:

  • 试试time 而不是clock
  • 谢谢,但我想知道多久了。只有几秒钟。如果时间过去了 3 秒,我想标记 3 秒。

标签: c xcode time


【解决方案1】:

您需要知道您的时钟功能正在读取的时间戳计数器寄存器。如果它是 32 位寄存器,它将非常频繁地复位。
例如,如果您的 CPU 有 2GHz 时钟,那么 1 sec = 2 * 1e9 cycles 和 32 位无符号寄存器可以从 0 to 2^32 - 1 周期计数。等于(2^32)/(2*1e9) = 2.14748365 seconds
在这种情况下,您的输出将始终限于2.14 seconds

【讨论】:

    猜你喜欢
    • 2015-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多