下面是一个求从100到999内的水仙花数的程序,算30000遍,只在算第一遍时在屏幕上输出结果。
这是VB的代码:
'VB *************************************
Private Sub Form_Paint()
Dim iVspace As Long
Dim iN1 As Long, iN2 As Long, iN3 As Long, iNs As Long
Dim N As Long
Dim I As Long, J As Long
For J = 0 To 30000
N = 1
For I = 100 To 999
iN1 = I \ 100
iN2 = (I - iN1 * 100) \ 10
iN3 = I - iN1 * 100 - iN2 * 10
iNs = iN1 * iN1 * iN1 + iN2 * iN2 * iN2 + iN3 * iN3 * iN3
If iNs = I Then
If J = 0 Then Form1.Print "第" & N & "个水仙花数是:" & I & vbCrLf
N = N + 1
End If
Next
Next
MsgBox "OK"
End Sub
'*******************************************这是VC的代码:
//******************************************
void CHelloView::OnDraw(CDC* pDC)
{
CHelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int iVspace=0;
int iN1,iN2,iN3,iNs;
int iN=1;
CString strOut;
int j=0;
for (j=0;j<30000;j++){
for (int i=100;i<1000;i++){
iN1=i/100;
iN2=(i-iN1*100)/10;
iN3=i-iN1*100-iN2*10;
iNs=iN1 * iN1 * iN1 + iN2 * iN2 * iN2 + iN3 * iN3 * iN3;
if (iNs==i){
if(j==0){
strOut.Format("第%d个水仙花数是:%d",iN,i);
pDC->TextOut(10,iVspace,strOut);
}
iN++;iVspace+=17;
}
}
}
}
//*****************************************************************分别编译后运行(注意一定要编译后运行!)
VB的计算速度比VC快上至少3倍! 谁知道这是怎么回事?
到底VB还是VC的效率高?
这是VB的代码:
'VB *************************************
Private Sub Form_Paint()
Dim iVspace As Long
Dim iN1 As Long, iN2 As Long, iN3 As Long, iNs As Long
Dim N As Long
Dim I As Long, J As Long
For J = 0 To 30000
N = 1
For I = 100 To 999
iN1 = I \ 100
iN2 = (I - iN1 * 100) \ 10
iN3 = I - iN1 * 100 - iN2 * 10
iNs = iN1 * iN1 * iN1 + iN2 * iN2 * iN2 + iN3 * iN3 * iN3
If iNs = I Then
If J = 0 Then Form1.Print "第" & N & "个水仙花数是:" & I & vbCrLf
N = N + 1
End If
Next
Next
MsgBox "OK"
End Sub
'*******************************************这是VC的代码:
//******************************************
void CHelloView::OnDraw(CDC* pDC)
{
CHelloDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
int iVspace=0;
int iN1,iN2,iN3,iNs;
int iN=1;
CString strOut;
int j=0;
for (j=0;j<30000;j++){
for (int i=100;i<1000;i++){
iN1=i/100;
iN2=(i-iN1*100)/10;
iN3=i-iN1*100-iN2*10;
iNs=iN1 * iN1 * iN1 + iN2 * iN2 * iN2 + iN3 * iN3 * iN3;
if (iNs==i){
if(j==0){
strOut.Format("第%d个水仙花数是:%d",iN,i);
pDC->TextOut(10,iVspace,strOut);
}
iN++;iVspace+=17;
}
}
}
}
//*****************************************************************分别编译后运行(注意一定要编译后运行!)
VB的计算速度比VC快上至少3倍! 谁知道这是怎么回事?
到底VB还是VC的效率高?
只靠这样一个简单的程序在判断VB和VC的效率与速度,是极为不全面也不科学的!
VC编译时你做优化了吗?你用的是 Win32Debug 还是 Win32Release ?