关于计算程序运行时间

vegetablebirdck 2011-09-28 01:25:02
为什么运行时间总是为0呢?到底哪里出错了呢?请大家指教。
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int a[100000];
long start=clock(),end(0);

for(int i=0;i<100000;i++)
a[i]=i;

for(int i=0;i<100000;i++)
sum+=a[i];
end=clock();

long result=(end-start)/1000;
cout<<"The time was: "<<result<<endl;

system("pause");

return 0;

}
...全文
82 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
hondely 2011-09-29
  • 打赏
  • 举报
回复
精度不够
vegetablebirdck 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 mscf 的回复:]
别除1000,要么就除1000F并且直接不赋值打印double值
[/Quote]


1000F?不行啊
vegetablebirdck 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 yxriyin 的回复:]
时间太短了吧,end-strat<1000
你用double result = (end-start)*1.0/1000试试
[/Quote]

试过了,不行啊
pathuang68 2011-09-29
  • 打赏
  • 举报
回复
[Quote=引用楼主 vegetablebirdck 的回复:]
为什么运行时间总是为0呢?到底哪里出错了呢?请大家指教。
C/C++ code
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
int a[100000];
long start=clock(),end(0);

for(int i=0;i<100000;i++)
……
[/Quote]

原因:
1. 这样的计算量对C++而言太小菜了,很快就算完了,建议在两个循环外面再加一层到两层循环。
2. long result=(end-start)/1000;就不要再除以1000变成秒了,直接用毫秒不是挺好的嘛,就算end-start不是零,但由于计算量太少,因此肯定会小于1000的,比如2ms,你再除以1000,也就变成0了。
wulj_0614 2011-09-28
  • 打赏
  • 举报
回复
别用clock,计算精确时间应该用QueryPerformanceFrequency和QueryPerformanceCounter。

#include <iostream>
#include <Windows.h>
using namespace std;

int main()
{
int a[100000];
int sum = 0;

LARGE_INTEGER fr;
QueryPerformanceFrequency(&fr);
LARGE_INTEGER t1, t2;
QueryPerformanceCounter(&t1);
for(int i=0;i<100000;i++)
a[i]=i;

for(int i=0;i<100000;i++)
sum+=a[i];
QueryPerformanceCounter(&t2);

long result = static_cast<long>((t2.QuadPart - t1.QuadPart) * 1000000 / fr.QuadPart);
cout<<"The time was: "<<result<<"us"<<endl;

system("pause");

return 0;

}

一叶之舟 2011-09-28
  • 打赏
  • 举报
回复
别除1000,或是用更高精度的计时器,精确到微秒或纳秒。
薛定谔之死猫 2011-09-28
  • 打赏
  • 举报
回复
别除1000,要么就除1000F并且直接不赋值打印double值
龙哥依旧 2011-09-28
  • 打赏
  • 举报
回复
// crt_clock.c
// This example prompts for how long
// the program is to run and then continuously
// displays the elapsed time for that period.
//

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void sleep( clock_t wait );

int main( void )
{
long i = 6000000L;
clock_t start, finish;
double duration;

// Delay for a specified time.
printf( "Delay for three seconds\n" );
sleep( (clock_t)3 * CLOCKS_PER_SEC );
printf( "Done!\n" );

// Measure the duration of an event.
printf( "Time to do %ld empty loops is ", i );
start = clock();
while( i-- )
;
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
printf( "%2.1f seconds\n", duration );
}

// Pauses for a specified number of milliseconds.
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}

Delay for three seconds
Done!
Time to do 6000000 empty loops is 0.1 seconds
yxriyin 2011-09-28
  • 打赏
  • 举报
回复
时间太短了吧,end-strat<1000
你用double result = (end-start)*1.0/1000试试

65,184

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧