Rog*_*ger 194 c benchmarking
我有一个C程序,旨在在几个处理器上并行运行.我需要能够记录执行时间(可能是1秒到几分钟).我已经搜索了答案,但他们似乎都建议使用该clock()函数,然后计算程序所用的时钟数除以该Clocks_per_second值.
我不确定如何Clocks_per_second计算价值?
在Java中,我只是在执行之前和之后以毫秒为单位.
C中有类似的东西吗?我看过了,但我似乎无法找到比第二种解决方案更好的方法.
我也知道分析器是一个选项,但我希望自己实现一个计时器.
谢谢
Tho*_*nin 315
CLOCKS_PER_SEC是一个声明的常量<time.h>.要获取C应用程序中任务使用的CPU时间,请使用:
clock_t begin = clock();
/* here, do your time-consuming job */
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
Run Code Online (Sandbox Code Playgroud)
请注意,这会将时间作为浮点类型返回.这可能比一秒钟更精确(例如,您测量4.52秒).精度取决于架构; 在现代系统上你很容易获得10毫秒或更低,但在较旧的Windows机器上(从Win98时代)它接近60毫秒.
clock()是标准C; 它"无处不在".有系统特定的功能,例如getrusage()类Unix系统.
Java System.currentTimeMillis()并没有衡量同样的事情.它是一个"挂钟":它可以帮助您测量程序执行所花费的时间,但它并不能告诉您使用了多少CPU时间.在多任务系统(即所有这些系统)上,这些系统可能大不相同.
S..*_*..K 107
如果您使用Unix shell运行,则可以使用time命令.
干
$ time ./a.out
Run Code Online (Sandbox Code Playgroud)
假设a.out,因为可执行文件将给你运行它的时间
Wes*_*ker 58
你在功能上想要这个:
#include <sys/time.h>
struct timeval tv1, tv2;
gettimeofday(&tv1, NULL);
/* stuff to do! */
gettimeofday(&tv2, NULL);
printf ("Total time = %f seconds\n",
(double) (tv2.tv_usec - tv1.tv_usec) / 1000000 +
(double) (tv2.tv_sec - tv1.tv_sec));
Run Code Online (Sandbox Code Playgroud)
请注意,这是以微秒为单位,而不仅仅是秒.
Ale*_* C. 54
在普通香草C:
#include <time.h>
#include <stdio.h>
int main()
{
clock_t tic = clock();
my_expensive_function_which_can_spawn_threads();
clock_t toc = clock();
printf("Elapsed: %f seconds\n", (double)(toc - tic) / CLOCKS_PER_SEC);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
adi*_*moh 12
大多数简单程序的计算时间以毫秒为单位.所以,我想,你会发现这很有用.
#include <time.h>
#include <stdio.h>
int main(){
clock_t start = clock();
// Execuatable code
clock_t stop = clock();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("Time elapsed in ms: %f", elapsed);
}
Run Code Online (Sandbox Code Playgroud)
如果要计算整个程序的运行时并且在Unix系统上,请使用如下的time命令运行程序time ./a.out
小智 9
(这里没有所有答案,如果您的系统管理员更改了系统时间,或者您的时区有不同的冬季和夏季时间。因此...)
linux使用:clock_gettime(CLOCK_MONOTONIC_RAW, &time_variable);
系统管理员更改时间,或您居住在冬季与夏季时间不同的国家等不受影响。
#include <stdio.h>
#include <time.h>
#include <unistd.h> /* for sleep() */
int main() {
struct timespec begin, end;
clock_gettime(CLOCK_MONOTONIC_RAW, &begin);
sleep(1); // waste some time
clock_gettime(CLOCK_MONOTONIC_RAW, &end);
printf ("Total time = %f seconds\n",
(end.tv_nsec - begin.tv_nsec) / 1000000000.0 +
(end.tv_sec - begin.tv_sec));
}
Run Code Online (Sandbox Code Playgroud)
man clock_gettime 状态:
CLOCK_MONOTONIC
Clock that cannot be set and represents monotonic time since some unspecified starting point. This clock is not affected by discontinuous jumps in the system time
(e.g., if the system administrator manually changes the clock), but is affected by the incremental adjustments performed by adjtime(3) and NTP.
Run Code Online (Sandbox Code Playgroud)
很多答案一直在建议clock(),然后CLOCKS_PER_SEC来自time.h.这可能是一个坏主意,因为这是我的/bits/time.h文件所说的:
/* ISO/IEC 9899:1990 7.12.1: <time.h>
The macro `CLOCKS_PER_SEC' is the number per second of the value
returned by the `clock' function. */
/* CAE XSH, Issue 4, Version 2: <time.h>
The value of CLOCKS_PER_SEC is required to be 1 million on all
XSI-conformant systems. */
# define CLOCKS_PER_SEC 1000000l
# if !defined __STRICT_ANSI__ && !defined __USE_XOPEN2K
/* Even though CLOCKS_PER_SEC has such a strange value CLK_TCK
presents the real value for clock ticks per second for the system. */
# include <bits/types.h>
extern long int __sysconf (int);
# define CLK_TCK ((__clock_t) __sysconf (2)) /* 2 is _SC_CLK_TCK */
# endif
Run Code Online (Sandbox Code Playgroud)
因此CLOCKS_PER_SEC可能被定义为1000000,具体取决于您用于编译的选项,因此它似乎不是一个好的解决方案.
Thomas Pornin作为宏的答案:
#define TICK(X) clock_t X = clock()
#define TOCK(X) printf("time %s: %g sec.\n", (#X), (double)(clock() - (X)) / CLOCKS_PER_SEC)
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
TICK(TIME_A);
functionA();
TOCK(TIME_A);
TICK(TIME_B);
functionB();
TOCK(TIME_B);
Run Code Online (Sandbox Code Playgroud)
输出:
time TIME_A: 0.001652 sec.
time TIME_B: 0.004028 sec.
Run Code Online (Sandbox Code Playgroud)
#include<time.h>
#include<stdio.h>
int main(){
clock_t begin=clock();
int i;
for(i=0;i<100000;i++){
printf("%d",i);
}
clock_t end=clock();
printf("Time taken:%lf",(double)(end-begin)/CLOCKS_PER_SEC);
}
Run Code Online (Sandbox Code Playgroud)
这个程序将会像魅力一样发挥作用。