我是 C 编程新手,我编写了代码并希望获得它的运行时。这就是我所做的。每次运行代码时,我都会得到不同的运行时值。这样对吗?或者我的代码有问题吗?
int main(int argc, char *argv[])
{
time_t start,end;
start=clock();
// this part is some operation
end=clock();
int running_time=end-start;
printf("Time taken: %d",running_time);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
小智 2
运行时间发生变化是很正常的,因为您的计算机可能正在运行许多东西(包括操作系统),而不仅仅是那段代码,这会影响时钟速度。
在处理消耗非常稳定的计算机上,运行时间应该保持相似。
你的代码没有任何问题。