时差为零秒 [英] time difference is zero sec

查看:40
本文介绍了时差为零秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里我试图计算完成合并排序所需的时间.但是开始和结束之间的差异显示零秒.我不知道是什么问题.为了方便起见,我只发布主函数在哪里时间计算.

Here i am trying to count to calculate time required to complete merge sort.But the difference between start and end is showing zero secend.I don't know what is the problem.For convenience i am posting only the main function where time is calculated.

#include<stdio.h>
#include<time.h>
int main(){
    clock_t start,end,diff;
    start=clock();
    int arr[4]={12,2,56,1};
    int i;
    printf("beforn sort\n");
    printf("\n-------------\n");
    for(i=0;i<4;i++){
        printf("%d ",arr[i]);
    }
    printf("\n \n");
    Merge_sort(arr,0,3);
    printf("after merge sort\n");
    printf("\n-------------\n");
    for(i=0;i<4;i++){
        printf("%d ",arr[i]);
    }
    printf("\n");
    end=clock();
    diff=(double)(end-start)/CLOCKS_PER_SEC;
    printf("total time is %f sec ",diff);
}

推荐答案

clock() 返回自程序启动以来经过的时钟滴答数.这样

clock() returns the number of clock ticks elapsed since the program was launched. So that

start=clock();

给出从程序启动到 clock() 被调用的时钟滴答数.它在排序之前为您提供时钟滴答.这是时钟滴答数,而不是秒数.

gives number of clock ticks from program launched till clock() is called. Which gives you clock ticks before sorting.This is number of clock ticks, not seconds.

排序后

end=clock()

给出从程序启动到 clock() 被调用的时钟滴答数.排序后为您提供时钟滴答.这是时钟滴答数,而不是秒数.

gives number of clock ticks from program launched till clock() is called. Which gives you clock ticks after sorting.This is number of clock ticks, not seconds.

现在 end-start 给出排序过程中的时钟滴答数.(这也不是以秒为单位)

Now end-start gives number of clock ticks during sorting process.(This also is not in seconds)

(时钟滴答数)/(一秒内时钟滴答数)= 时间(以秒为单位)

(Number of clock ticks)/(Number of clock ticks in one second)= time in seconds

(end-start)/CLOCKS_PER_SEC 给出排序过程所需的时间(以秒为单位).但是在 C 中,这给出了一个整数.所以它必须被类型转换为双精度.这给了.

(end-start)/CLOCKS_PER_SEC gives time required for the sorting process in seconds. But in C this gives an integer. So it has to typecasted to double for precision. That gives.

double diff;
diff=(double)(end-start)/CLOCKS_PER_SEC;

这篇关于时差为零秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆