将 time_t 设置为毫秒 [英] Set time_t to milliseconds

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

问题描述

我有一个函数,我希望该函数在运行了一定的毫秒数后停止运行.这个函数可以工作几秒钟,但我想测试它几毫秒.我该怎么做呢?如果我设置消除 = 1,它对应于 1 秒.我如何设置消除 = 5 毫秒?

I have a function and I want the function to stop running once it has been running for a certain number of milliseconds. This function works for seconds but I want to test it for milliseconds. How do I do this? If I set eliminate = 1, it corresponds to 1 second. How do I set eliminate = 5 ms?

功能:

void clsfy_proc(S_SNR_TARGET_SET pSonarTargetSet, unsigned char *target_num, time_t eliminate)
{

    // get timing
    time_t _start = time(NULL);
    time_t _end = _start + eliminate;
    int _eliminate = 0;

    //some code

        time_t start = time(NULL);
        time_t end = start + eliminate;

        for(_tidx = 0; _tidx < pSonarTargetSet[_i].num; _tidx++) {
            // check timing
            time_t _current = time(NULL);
            if (_current > _end) {
                printf("clsfy_proc(1), Eliminate due to timeout\n");
                _eliminate = 1;
                break;
            }

            //some code 

        if (_eliminate == 1)
            break;
    }
    //some code 
}

推荐答案

time_t 是一个绝对时间,表示为自 UNIX 纪元(格林威治标准时间,1970 年 1 月 1 日午夜)以来的整数秒数.它作为时间点的明确、易于使用的表示非常有用.

time_t is an absolute time, represented as the integer number of seconds since the UNIX epoch (midnight GMT, 1 January 1970). It is useful as an unambiguous, easy-to-work-with representation of a point in time.

clock_t 是时间的相对度量,由自某个时间点以来的整数个时钟滴答表示(可能是计算机启动,但不能保证,因为它可能经常翻转).每秒有 CLOCKS_PER_SEC 时钟滴答;这个常量的值在不同的操作系统之间会有所不同.它有时用于计时目的,但由于其相对较低的分辨率而不太擅长.

clock_t is a relative measurement of time, represented by an integer number of clock ticks since some point in time (possibly the computer's bootup, but no guarantees, as it may roll over quite often). There are CLOCKS_PER_SEC clock ticks per second; the value of this constant can vary between different operating systems. It is sometimes used for timing purposes, but it is not very good at it due to its relatively low resolution.

clock_t 的一个小例子:

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

int main () {
   clock_t start_t, end_t, total_t;
   int i;

   start_t = clock();
   printf("Starting of the program, start_t = %ld\n", start_t);

   for(i=0; i< 10000000; i++) { }

   end_t = clock();
   printf("End of the big loop, end_t = %ld\n", end_t);

   total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
   printf("Total time taken by CPU: %f\n", total_t  );

   return(0);
}

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

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