clock()与time()区别

本文详细介绍了C语言中的两个常用时间函数:clock() 和 time()。clock() 函数用于获取程序运行过程中消耗的CPU时间,以系统特定的时钟刻度为单位;time() 函数则用于获取自1970年1月1日以来的秒数,常用于记录实际的时间流逝。文章对比了这两个函数的不同应用场景,并给出了使用建议。

1. clock

clock_t clock (void);

Clock program
Returns the processor time consumed by the program.

The value returned is expressed in clock ticks,
which are units of time of a constant but system-specific length 
(with a relation of CLOCKS_PER_SEC clock ticks per second).

The epoch used as reference by clock varies between systems,
but it is related to the program execution (generally its launch). 
To calculate the actual processing time of a program, 
the value returned by clock shall be compared to a value 
returned by a previous call to the same function.

2. time

time_t time (time_t* timer);
Get current time
Get the current calendar time as a value of type time_t.

The function returns this value, 
and if the argument is not a null pointer, 
it also sets this value to the object pointed by timer.

The value returned generally represents the number of seconds 
since 00:00 hours, Jan 1, 1970 UTC (i.e., the current unix timestamp). 
Although libraries may use a different representation of time: 
Portable programs should not use the value returned by this function directly, 
but always rely on calls to other elements of the standard library 
to translate them to portable types (such as localtime, gmtime or difftime).

3. 区别

举例
结果
clock()返回从“开启这个程序进程”到“程序中调用clock()函数”时之间的CPU时钟计时单元(clock tick)数,sleep(5)并不占用cpu资源,
导致start1和end1返回的值一样。
time(&temp)返回从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。
总之,用time_t计时才是人们正常意识上的秒数,而clock_t计时所表示的是占用CPU的时钟单元。

参考文献:
【1】http://www.cplusplus.com/reference/ctime/time/?kw=time
【2】http://blog.youkuaiyun.com/xiaofei2010/article/details/8489014 作者:xiaofei2010

### 时钟到达时间概念 在数字电路设计中,时钟到达时间 (Clock Arrival Time) 是指时钟信号从源出发并传播到目标寄存器所需的时间。这一参数对于同步逻辑系统的正常运行至关重要。 为了确保数据能够在正确的时刻被采样,必须精确控制各个组件之间的相对时序关系。这涉及到构建合理的时钟树结构来分配多个副本给不同位置的目标设备[^1]。 #### 计算方法 计算具体路径上的时钟到达时间涉及以下几个因素: - **时钟源延迟**:这是由振荡器或其他初始时钟提供者引入的基础延时。 - **缓冲级联影响**:每当经过一个额外的缓冲阶段时,都会增加一定的传输延迟。这些增量取决于所使用的特定类型的缓冲元件及其物理特性。 - **互连线长度引起的延迟**:金属线迹本身的电阻和电容也会造成信号传递过程中的衰减滞后现象。 因此,在实际应用中,可以通过以下方式估算某一点处的总时钟到达时间 \( T_{arrival} \): \[T_{arrival}(i)=T_{source\_delay}+\sum _{j=1}^{n}{D_j(i)}\] 其中, - \( n \) 表示该节点之前经历过的全部中间环节数目; - \( D_j(i) \) 则代表第 j 层次上对应分支带来的附加延迟量; 此公式适用于理想情况下的理论分析,而在真实环境中还需要考虑温度变化、工艺偏差等因素的影响。 ```cpp // C++ code snippet demonstrating how to calculate Clock Arrival Time double CalculateArrivalTime(double sourceDelay, std::vector<double> delays){ double arrivalTime = sourceDelay; for(auto delay : delays){ arrivalTime += delay; // Add each buffer/interconnect delay sequentially } return arrivalTime; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值