一个在VC6和VC.net结果不一样的小程序
#include<iostream>
#include<ctime>
using namespace std;
int Binom(int n,int m)
{
if (m==0||n==m) return 1;
return Binom(n-1,m)+Binom(n-1,m-1);
}
int main()
{
clock_t start, finish;
long double duration;
unsigned long result;
for (int i=1;i<=15;i++)
{
start=clock();
result=Binom(2*i,i);
finish=clock();
duration = (long double)(finish - start) / CLOCKS_PER_SEC;
cout<<"Binom("<<i*2<<','<<i<<")="<<result<<" 所用的时间的为:"<<duration<<endl<<endl;;
}
}
这是我们的一道作业题,要比较两种计算组合数的算法,这是其中一个算法,用递归,我想用<ctime>函数输出所用的时间,可在VC.net下时间全是0,而在VC6就没问题。如果大家有测试程序执行时间的软件,还请相告。
问题点数:100、回复次数:5Top
1 楼ckacka(/*小红帽*/ckacka();)回复于 2003-04-04 00:32:58 得分 50
我这里没有问题
dev-c++
------------------
Binom(2,1)=2 所用的时间的为:0
Binom(4,2)=6 所用的时间的为:0
Binom(6,3)=20 所用的时间的为:0
Binom(8,4)=70 所用的时间的为:0
Binom(10,5)=252 所用的时间的为:0
Binom(12,6)=924 所用的时间的为:0
Binom(14,7)=3432 所用的时间的为:0
Binom(16,8)=12870 所用的时间的为:0
Binom(18,9)=48620 所用的时间的为:0.01
Binom(20,10)=184756 所用的时间的为:0.01
Binom(22,11)=705432 所用的时间的为:0.05
Binom(24,12)=2704156 所用的时间的为:0.21
Binom(26,13)=10400600 所用的时间的为:0.781
Binom(28,14)=40116600 所用的时间的为:3.024
Binom(30,15)=155117520 所用的时间的为:12.558
请按任意键继续 . . .
vc.net
------------------
Binom(2,1)=2 所用的时间的为:0
Binom(4,2)=6 所用的时间的为:0
Binom(6,3)=20 所用的时间的为:0
Binom(8,4)=70 所用的时间的为:0
Binom(10,5)=252 所用的时间的为:0
Binom(12,6)=924 所用的时间的为:0
Binom(14,7)=3432 所用的时间的为:0
Binom(16,8)=12870 所用的时间的为:0.01
Binom(18,9)=48620 所用的时间的为:0.03
Binom(20,10)=184756 所用的时间的为:0.11
Binom(22,11)=705432 所用的时间的为:0.4
Binom(24,12)=2704156 所用的时间的为:1.533
Binom(26,13)=10400600 所用的时间的为:5.868
Binom(28,14)=40116600 所用的时间的为:22.873
Binom(30,15)=155117520 所用的时间的为:93.104
请按任意键继续 . . .
Top
2 楼robertnet(大师兄)回复于 2003-04-04 00:59:34 得分 20
没问题Top
3 楼arya(行者)回复于 2003-04-04 01:30:42 得分 30
clock函数不能测小于1/18.2=54.9毫秒的时间间隙。如果所测时间小于这个间隙,就是0或者无意义.Top
4 楼Macosx(结贴)回复于 2003-04-04 10:04:44 得分 0
to ckacka(小红帽) :
为什么两个编译器下程序执行的时间差那么多,我的机器算Binom(30,15)时只要7秒多(感觉)。可是即使这样,.net下显示结果也是0。
to arya(行者) :
谢谢指点,不知有没有更好办法测时间?
Top
5 楼Macosx(结贴)回复于 2003-04-04 10:10:43 得分 0
终于找到原因了,原来我的编译时用了release,改成debug就没问题了,谢谢各位指点。Top




