好的,现在彻底改变了我的问题,我已经找到了时间问题。所以现在这就是我的代码;
int main()
{
int start_s = clock();
int i, n = 10;
int sum = 0;
for (i = 0; i < n; ++i)
{
cout << "Sum: " << sum << "\n";
++sum;
int stop_s = clock();
cout << "time: " << (stop_s - start_s) / double(CLOCKS_PER_SEC) * 1000 << endl;
system("pause");
}
}
但是现在我的问题是执行时间显示在怪异的数字中,例如我第一次运行时我会说时间:1,然后再次运行它会变得像时间一样奇怪:4000等等。我如果可能的话,想要时间以毫秒为单位,或者等待0.0043的时间。
感谢您的帮助!
答案 0 :(得分:1)
您必须将(stop_s - start_s)
投射为双倍。请看下面的代码。
t = (stop_s - start_s);
double time_taken = (((double)t)/CLOCKS_PER_SEC) * 1000; //time in milliseconds
int main()
{
int start_s = clock();
int i, n = 10;
int sum = 0;
for (i = 0; i < n; ++i)
{
cout << "Sum: " << sum << "\n";
++sum;
int stop_s = clock();
double t = double (stop_s - start_s);
cout << "time: " << (t / double(CLOCKS_PER_SEC)) * 1000 << endl;
system("pause");
}
}
这应该有效,但我现在无法测试。