C 在 linux 中使用 openssl 加密时如何测量经过的时间

How can I measure elapsed time when encrypting using openssl in linux by C

如何计算 Linux 上 C 中进程使用的处理时间。具体来说,我想确定使用 openssl 加密文件时经过了多少时间。

最简单的方法是使用 <time.h> 中的 clock() 函数来报告调用进程使用的 CPU 时间量。

来自SUSv4

The clock() function shall return the implementation's best approximation to the processor time used by the process since the beginning of an implementation-defined era related only to the process invocation.

RETURN VALUE

To determine the time in seconds, the value returned by clock() should be divided by the value of the macro CLOCKS_PER_SEC. If the processor time used is not available or its value cannot be represented, the function shall return the value (clock_t)-1.

尝试关注,

 time_t start, end;
 double cpu_time_used;

 start = clock();
 /* Do encrypting ... */
 end = clock();
 cpu_time_used = ((double) (end - start)) / CLOCKS_PER_SEC;