使用clock_gettime在C中计时浮点计算,时间在不断变化?

时间:2014-05-14 14:12:15

标签: c floating-point timing

我正在尝试使用clock_gettime()计算一段代码(浮点乘法)的时间。我随机生成10个100000个浮点数的数组,然后将它们全部加在一起,一次十个。我想要做的是测量执行十次浮点乘法所需的时间,然后将该时间值添加到总时间,然后在结束时将它们全部打印出来。

我认为这一切都有效,但我发现如果从while循环中删除printf()语句,则时间会从28125692减少到17490394纳秒!这不应该发生。我将clock_gettime()调用放在浮点乘法的开头和结尾处,所以从理论上讲,printf()语句不应该影响经过的时间!

有什么想法吗?谢谢。

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

int main(int argc, char *argv[])
{
    int i;
    int j;
    float alpha[100000];
    float bravo[100000];
    float charlie[100000];
    float delta[100000];
    float echo[100000];
    float foxtrot[100000];
    float golf[100000];
    float hotel[100000];
    float india[100000];
    float juliet[100000];
    float max;
    long long num_calcs;
    struct timespec start_time;
    struct timespec end_time;
    int diff_seconds;
    long diff_nanoseconds;
    long long total_calcs;
    long long total_seconds;
    long long total_nanoseconds;

    num_calcs = 100000;

    max = 1000.0;

    printf("\n%lld floating point calculations requested.", num_calcs);
    printf("\nGenerating random floating point values...");

    //initialize random seed
    srand((unsigned int)time(NULL));

    //generate random floating point values
    for (i = 0; i < 100000; i++)
    {
        alpha[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        bravo[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        charlie[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        delta[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        echo[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        foxtrot[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        golf[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        hotel[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        india[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
        juliet[i] = ((float) rand()) / ((float) (RAND_MAX)) * max;
    }
    printf("done!");

    printf("\nRunning %lld floating point multiplications...", num_calcs);

    //run calculations
    i = 0;
    total_calcs = 0;
    total_seconds = 0;
    total_nanoseconds = 0;

    while (total_calcs < num_calcs)
    {
        printf("\n\nRunning 10 calculations...");
        //do 10 multiplications
        //start the timer
        if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &start_time) < 0)
        {
            printf("\nclock_gettime for start_time failed, exiting...");
            return -1;
        }
        alpha[i] * bravo[i] * charlie[i] * delta[i] * echo[i] *
        foxtrot[i] * golf[i] * hotel[i] * india[i] * juliet[i];
        //stop the timer
        if (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &end_time) < 0)
        {
            printf("\nclock_gettime for end_time failed, exiting...");
            return -1;
        }
        printf("done!");
        total_calcs = total_calcs + 10; //increase total calculations
        i++;                //increment array index

        //show timing statistics
        printf("\nSTART TIME tv_sec:  %d", (int) start_time.tv_sec);
        printf("\nSTART TIME tv_nsec: %ld", start_time.tv_nsec);
        printf("\nEND TIME tv_sec:    %d", (int) end_time.tv_sec);
        printf("\nEND TIME tv_nsec:   %ld", end_time.tv_nsec);

        //calculate time it took to run 10 floating point caculcations
        if ((end_time.tv_nsec - start_time.tv_nsec) < 0)
        {
            diff_seconds = end_time.tv_sec - start_time.tv_sec - 1;
            diff_nanoseconds = 1000000000 + end_time.tv_nsec - start_time.tv_nsec;
        }
        else
        {
            diff_seconds = end_time.tv_sec - start_time.tv_sec;
            diff_nanoseconds = end_time.tv_nsec - start_time.tv_nsec;
        }

        //add elapsed time for the last 10 calculations to total elapsed time
        total_seconds = total_seconds + diff_seconds;
        total_nanoseconds = total_nanoseconds + diff_nanoseconds;

        printf("\nPerformed 10 floating point multiplications in %d seconds and %ld nanoseconds.", diff_seconds, diff_nanoseconds);
        printf("\nPerformed %lld floating point multiplications in %lld seconds and %lld nanoseconds.", total_calcs, total_seconds, total_nanoseconds);
    }
    printf("done!");

    printf("\nPerformed %lld floating point multiplications in %lld seconds and %lld nanoseconds.\n", total_calcs, total_seconds, total_nanoseconds);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

声明

    alpha[i] * bravo[i] * charlie[i] * delta[i] * echo[i] *
    foxtrot[i] * golf[i] * hotel[i] * india[i] * juliet[i];

是无操作。优化编译器将从可执行文件中省略它。

要使其正常工作,请尝试将结果添加到另一个变量,并在程序结束时打印总和。