-1

返回什么time(NULL)?给定文档,我假设它返回自纪元以来的秒数,但是,当我运行以下代码时:

#include <iostream>
#include "Time.h"
#include "Math.h"

int main () {

    double curT = 0;
    double curJ = 0;

    time_t curTtimeT = 0;
    double curJtimeT = 0;

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++) {

        //get the current time
        curT = time(NULL);
        curTtimeT = time(NULL);

        //check if current time equals previous time
        if (time(NULL) != curT) {
            //output time and approx. "time" through iteration
            std::cout << "Time match with doubles:\t" << curT << "\tdeltaJ:\t\t\t" << (j - curJ) <<std::endl;
            curT = time(NULL);
            curJ = j;
        }

        if (time(NULL) != curTtimeT) {
            //output time and approx. "time" through iteration
            std::cout << "Time match with time_t:\t\t" << curTtimeT << "\t\tdeltaJtimeT:\t\t" << (j - curJtimeT) <<std::endl;
            curTtimeT = time(NULL);
            curJtimeT = j;
        }

    }
    return 0;
}

我得到以下结果(跳过似乎在运行之间没有模式):

Time match with time_t:     1348873002      deltaJtimeT:        290842
Time match with doubles:    1.34887e+09 deltaJ:         2.41017e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08409e+06
Time match with doubles:    1.34887e+09 deltaJ:         2.16587e+06
Time match with time_t:     1348873007      deltaJtimeT:        5.36928e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08696e+06
Time match with time_t:     1348873008      deltaJtimeT:        1.08696e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08534e+06
Time match with doubles:    1.34887e+09 deltaJ:         3.18296e+06
...
Time match with time_t:     1348873122      deltaJtimeT:        2.16217e+06
Time match with doubles:    1.34887e+09 deltaJ:         6.42553e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.08727e+06
Time match with doubles:    1.34887e+09 deltaJ:         2.14147e+06
Time match with doubles:    1.34887e+09 deltaJ:         1.04733e+06
Time match with time_t:     1348873130      deltaJtimeT:        8.42965e+06

注意:我正在使用deltaJ输出以更数字化的方式显示在控制台实际输出此循环结果时的明显视觉差异。

显然,这似乎并没有始终如一地返回自纪元以来的秒数,因为我反而能够看到非常一致的输出,即每秒doubletime_t每秒都非常接近 1 行。1348873002相反,我错过了和之间的整个范围1348873007- 这些值似乎都没有通过time(NULL). 这些间隙在此循环的整个运行时始终出现。

1348873122此外,有时它甚至在某些秒内不输出(参见和之间的差距1348873130)。

我不明白为什么输出不显示两种变量类型的一个条目,对于每一秒的实时。相反,它似乎time(NULL)不一致地返回自纪元以来的秒数,实际上跳过了一些值。

我错过了什么?

我在 Snow Leopard 上的 X-Code 3.2.5 中的 2.4GHz 双核 Macbook Pro 上运行这些测试(也许这个问题特定于我的系统)?

4

5 回答 5

1

<time.h>time()

time_t time(time_t *res);

的类型time_t通常是有符号整数类型。

由于您使用"Time.h",您可能对time()完全有不同的定义。

我更喜欢<cstdio>得到整齐对齐和格式化的输出(我是 C 程序员而不是 C++ 程序员),但代码可以编译为:

#include <ctime>
#include <cstdio>
using namespace std;

int main()
{
    double curT = 0;
    double curJ = 0;

    time_t curTtimeT = 0;
    double curJtimeT = 0;

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++)
    { 
        //get the current time
        curT = time(NULL);
        curTtimeT = time(NULL);

        //check if current time equals previous time
        if (time(NULL) != curT) {
            printf("CurT:      %16.2f;  deltaJ: %16.2f\n", curT, j - curJ);
            curT = time(NULL);
            curJ = j;
        }

        if (time(NULL) != curTtimeT) {
            printf("CurTtimeT: %13lld;     deltaJ: %13lld\n", (long long)curTtimeT, (long long)(j - curJtimeT));
            curTtimeT = time(NULL);
            curJtimeT = j;
        }
    }
    return 0;
}

这会产生如下输出:

CurT:         1348875805.00;  deltaJ:       1215329.00
CurT:         1348875806.00;  deltaJ:       1403669.00
CurTtimeT:    1348875806;     deltaJ:       2618998
CurTtimeT:    1348875807;     deltaJ:       1395183
CurT:         1348875808.00;  deltaJ:       2815155.00
CurT:         1348875809.00;  deltaJ:       1401716.00
CurTtimeT:    1348875809;     deltaJ:       2821688
CurT:         1348875810.00;  deltaJ:       1410729.00
CurT:         1348875811.00;  deltaJ:       1411382.00
CurTtimeT:    1348875811;     deltaJ:       2822111
CurTtimeT:    1348875812;     deltaJ:       1420231
CurT:         1348875813.00;  deltaJ:       2840762.00
CurTtimeT:    1348875815;     deltaJ:       4242399
CurT:         1348875816.00;  deltaJ:       4231937.00

Cur*价值观是有意义的;这些deltaJ值是不相关的,因为您在循环计数器和时间值之间存在差异。我不确定你希望看到什么。

(MacBook Pro、2.3 GHz Intel Core i7、Mac OS X Lion 10.7.5、GCC 4.6.0——我需要在本周早些时候的灾难之后恢复我的 GCC 更高版本。)

于 2012-09-28T23:23:25.560 回答
1

显然,这似乎并没有始终如一地返回自纪元以来的秒数,相反,我将能够看到非常一致的输出,即每秒doubletime_t每秒都非常接近 1 行。

这不遵循。我认为您打算让您的代码与实际情况有所不同。

具体来说,我认为你需要改变这一点:

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++) {

        //get the current time
        curT = time(NULL);
        curTtimeT = time(NULL);

对此:

    //get the current time
    curT = time(NULL);
    curTtimeT = time(NULL);

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++) {

因为事实上,您有一种竞争条件:您正在测试循环的某些部分time是否更改了返回值。显然,这不会 100% 发生。有时第二个会在循环的一个部分翻转,而其他时候它会在另一个部分翻转。

于 2012-09-28T23:25:31.230 回答
1

我认为您的测试代码有点过于复杂。您的第一列看起来正确,但您没有显示所有数字,因此您看不到秒数。使用 time_t plus double 只会混淆输出。试试这个——它很笨拙,但它每秒都会显示新的时间。

#include <iostream>
#include "Time.h"

int main () {
    //get the current time
    time_t currtime = time(NULL);

    //run indefinitely... use a debugger you can stop :-)
    for (double j = 0; j < 2000000000; j++) {

        //wait until a second has passed
        while (time(NULL) < currtime + 1) {
        };
        currtime = time(NULL);
        std::cout << "Time: " << currtime << "\n";
    }
    return 0;
}
于 2012-09-28T23:36:18.730 回答
0

您在这里假设您的应用程序在没有任何干扰的情况下运行:您的系统是一个先发制人的系统,因此有时操作系统可能会与您的进程相交以处理其他事情:您的“时间跳跃”可能是因为这个(即使我是仍然好奇它是否会放弃您的过程 5 秒钟,这仍然可能发生)...

此外,我认为您的代码可能会因某些优化而导致您的代码工作方式有所不同:我建议检查编译器的设置并禁用优化以查看是否有任何区别......

您也可以尝试检查任何溢出,即使这不太可能,考虑到您的系统(64 位整数应该足以满足您在这里所做的工作)......

于 2012-09-28T23:25:35.553 回答
0

相反,我将能够看到非常一致的输出,即每秒 double 和 time_t 非常接近 1 行。

这正是您不应该期望看到的。您应该期望看到很多明显的随机性,而这正是您所看到的。循环的每次迭代都会进行两到六次调用,time()有时会生成格式化输出,有时不会。I/O 是昂贵的,格式化的 I/O 是非常昂贵的,系统调用如time():它们也很昂贵。

除了运行应用程序之外,您的计算机还在做许多其他事情。它正在创建需要在屏幕上显示的内容,它正在检查您的计算机是否需要更新,它正在运行一些防病毒程序,它正在更新一些您没有关闭的浏览器页面,等等。CPU 不断切换上下文在运行您的程序和运行无数其他活动程序之间。这些系统调用是切换上下文的理想场所(也是首选场所)。

在您的循环的每次迭代中有八个地方是您的程序邀请操作系统切换上下文的地方。看到很多变化并不奇怪。

于 2012-09-28T23:35:23.190 回答