V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
xdeng
V2EX  ›  C

想问 macOS 上 clock()函数的问题。

  •  
  •   xdeng · 2018-11-19 19:44:52 +08:00 · 2131 次点击
    这是一个创建于 1956 天前的主题,其中的信息可能已经有所发展或是发生改变。
    clock_t t = clock();
    sleep(10);
    t = clock() - t;
    printf ("It took me %d clicks (%f seconds).\n",t,((float)t)/CLOCKS_PER_SEC);

    在 macOS 上为什么不是接近于 10 而是 4.0-4.5 之间?
    就算不除以 CLOCKS_PER_SEC 也不是接近于 10 的数,在 QT 和 xcode 上测都是这样。
    4 条回复    2018-11-21 20:55:22 +08:00
    choury
        1
    choury  
       2018-11-19 20:01:02 +08:00   ❤️ 1
    你要明白,sleep 时进程会休眠的,不会占用 cpu 时间的
    xdeng
        2
    xdeng  
    OP
       2018-11-20 15:51:18 +08:00
    @choury 好像说的对 我稍后试下
    xdeng
        3
    xdeng  
    OP
       2018-11-21 20:29:27 +08:00
    @choury 试了在 Windows 上是运行时间,Linux 上市占用 cpu 时间。
    choury
        4
    choury  
       2018-11-21 20:55:22 +08:00 via Android   ❤️ 1
    @xdeng 是的,windows 的这个函数实现的有 bug,可以看文档
    https://msdn.microsoft.com/en-us/library/4e2ess30.aspx?f=255&MSPPError=-2147217396
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1000 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 19:59 · PVG 03:59 · LAX 12:59 · JFK 15:59
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.