注册 登录
编程论坛 VB6论坛

这个程序如何提高速度?算到10万内居然就需要几个小时呢

ysr2857 发布于 2025-03-11 13:27, 11327 次点击
Private Sub Command1_Click()
'这个程序如何提高速度?算到10万内就需要几个小时呢
Dim a, b
a = Val(Text1)

Do While x <= a
x = x + 1
y = 0
Do While y <= a
y = y + 1
b = x ^ 2 - 2029 * y ^ 2
If b = -487 Then
s = s + 1

js = js & "x=" & x & "  y=" & y & vbCrLf
Else
js = js
End If

Loop
Loop

If s > 0 Then
Text2 = js
Else
Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()
Text1 = ""
Text2 = ""

End Sub
19 回复
#2
风吹过b2025-03-11 19:26
1、明确指出变量数据类型,
Variant 数据类型是最慢,你可以把 a ,b ,s 定义为 long ,x,y ,定义为 Double ,因为计算过程中会超 long 范围。
long 数据类型对于VB6来说 是最快的,因为正好是计算机字长。

2、无效语句
js = js   这行命令干嘛?并且每个循环都要执行一遍

3、算法
核心计算是这句:b = x ^ 2 - 2029 * y ^ 2
这里的,B的值是随着 y 变大而逐步变小的,这里我们有一个结论,当
1、当 b = -487 成立时,不需要继续向后搜索了。
2、当 b < -487 成立时,也不需要继续向后搜索了。
所以这里加二个判断,跳过后续循环
If b = -487 Then
  s = s + 1
  js = js & "x=" & x & "  y=" & y & vbCrLf
  exit do
Elseif  b < -487 Then
  exit do
'else          '这二句都不要了
'js=js         '多余命令
End If

4、优化
每次内循环都要计算一遍 X ^ 2 ,这里浪费算力,可以在外循环里计算一次,然后保存到一个变量里,内循环直接使用。


'c 也要定义为 Double
Do While x <= a
x = x + 1
c = x ^ 2
y = 0
Do While y <= a
y = y + 1
b = c - 2029 * y ^ 2
......

----------------
测试时,VB6崩溃了,不愿再重做测试工程了。就这样吧。



[此贴子已经被作者于2025-3-11 19:27编辑过]

#3
ysr28572025-03-11 21:45
回复 2楼 风吹过b
谢谢!感谢指导,给你加分!

我试试,非常感谢!
#4
ysr28572025-03-11 23:52
Private Sub Command1_Click()
'这次速度快了不少以前10分钟的计算这次是1分钟了
Dim a, b, s As Long
Dim x, y As Double
a = Val(Text1)
x = 0
Do While x <= a
x = x + 1
y = 0
Do While y <= a
y = y + 1
b = x ^ 2 - 125 * y ^ 2
If b = -29 Then
  s = s + 1
  js = js & "x=" & x & "  y=" & y & vbCrLf
  Exit Do
ElseIf b < -29 Then
  Exit Do
'else          '这二句都不要了
'js=js         '多余命令
End If


Loop
Loop

If s > 0 Then
Text2 = js
Else
Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub
#5
ysr28572025-03-11 23:59
Private Sub Command1_Click()
'这次也快以前10分钟的计算这次是1分钟了
Dim a, b, s As Long
Dim c
Dim x, y As Double
a = Val(Text1)
x = 0
Do While x <= a
x = x + 1
c = x ^ 2
y = 0
Do While y <= a
y = y + 1
b = c - 125 * y ^ 2
If b = -29 Then
  s = s + 1
  js = js & "x=" & x & "  y=" & y & vbCrLf
  Exit Do
ElseIf b < -29 Then
  Exit Do
'else          '这二句都不要了
'js=js         '多余命令
End If


Loop
Loop

If s > 0 Then
Text2 = js
Else
Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub
#6
风吹过b2025-03-12 10:00
Dim a, b, s As Long
Dim c
Dim x, y As Double

定义不能这样写,应该显示指出每个的数据类型

Dim a As Long, b As Long, s As Long
Dim c As Double
Dim x As Double, y As Double


#7
风吹过b2025-03-12 17:46
其实,这种程序的逻辑有问题。
已知公式,已知一个变量值,可以直接求解另一个变量值,来判断是否符合要求,
为什么还要大海捞针的用双循环去跑呢。

b = c - 125 * y ^ 2
If b = -29 Then
-----------------------
for x = 1 to a
y = sqr( x * x - 125 + 29 )
if int(y)=y then         '得到是一个整数
  if y<a then        '在输入范围内
    '得到一个解
  end if
end if
next x
这样不是更快一点吗
没有环境,未经测试

#8
ysr28572025-03-13 07:21
回复 6楼 风吹过b
谢谢指导,我试试这个数据类型:
Dim c As Double
#9
ysr28572025-03-13 07:25
Private Sub Command1_Click()
'可以,5万内不到1分钟
Dim a, b, s As Long
Dim c As Double
Dim x, y As Double
a = Val(Text1)
x = 0
Do While x <= a
x = x + 1
c = x ^ 2
y = 0
Do While y <= a
y = y + 1
b = c - 125 * y ^ 2
If b = 29 Then
  s = s + 1
  js = js & "x=" & x & "  y=" & y & vbCrLf
  Exit Do
ElseIf b < 29 Then
  Exit Do
'else          '这二句都不要了
'js=js         '多余命令
End If


Loop
Loop

If s > 0 Then
Text2 = js
Else
Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub

#10
ysr28572025-03-13 07:26
回复 7楼 风吹过b
谢谢指导!我试试这个单循环的:!!!!!!!!!!!!!!!!
#11
ysr28572025-03-13 07:53
y = sqr( x * x - 125 + 29 )
咋是无效的调用过程或参数?
明白了加个绝对值!!

[此贴子已经被作者于2025-3-13 07:55编辑过]

#12
ysr28572025-03-14 06:30
Private Sub Command1_Click()
'可以,10万内不到1分钟
Dim a, b, s As Long
Dim c As Double
Dim x As Double
Dim y, jj

a = Val(Text1)
For x = 1 To a
y = Abs(x * x - 139) / 13
y = y ^ 0.5
If Int(y) = y Then       '得到是一个整数
  If y < a Then   '在输入范围内
    '得到一个解
    s = s + 1
    js = js & "x=" & x & "  y=" & y & vbCrLf
  End If
End If
Next x

If s > 0 Then
Text2 = js
Else
Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub
#13
cwa99582025-03-17 08:44
y = y ^ 0.5改为 y=sqr(y)
试试下面的代码:
程序代码:

Private Sub Command1_Click()
'可以,10万内不到1分钟
Dim a As Long, b As Long, s As Long
Dim c As Double
Dim x As Double
Dim y As Double, js As String

Text2 = ""
a = Val(Text1)
t = Timer
DoEvents

For x = 1 To a
    y = Abs(x * x - 139) / 13
    y = Sqr(y)
    If Int(y) = y Then       '得到是一个整数
      If y < a Then   '在输入范围内
        '得到一个解
        s = s + 1
        js = js & "x=" & x & "  y=" & y & vbCrLf
      End If
    End If
Next x

If s > 0 Then
    Text2 = js & vbCrLf & Timer - t
  Else
    Text2 = "  wu  jie"
End If


End Sub
#14
ysr28572025-03-17 09:21
x=7875  y=314
x=9851725  y=392814
x=438642128  y=17489807
x=778845506  y=31054604
x=892246632  y=35576203

50.96484
#15
ysr28572025-03-17 09:25
回复 13楼 cwa9958
非常感谢!已经结帖,记分用完了,没法给你得分了,抱歉!
感觉快了不少,原来的程序没有计时程序,修改后的程序代码如下:
Private Sub Command1_Click()
'可以,10万内不到1分钟
Dim a As Long, b As Long, s As Long
Dim c As Double
Dim x As Double
Dim y As Double, js As String

Text2 = ""
a = Val(Text1)
t = Timer
DoEvents

For x = 1 To a
    y = Abs(x * x + 1259) / 629
    y = Sqr(y)
    If Int(y) = y Then       '得到是一个整数
      If y < a Then   '在输入范围内
        '得到一个解
        s = s + 1
        js = js & "x=" & x & "  y=" & y & vbCrLf
      End If
    End If
Next x

If s > 0 Then
    Text2 = js & vbCrLf & Timer - t
  Else
    Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub
#16
ysr28572025-03-19 21:32
回复 15楼 ysr2857
只能算10000 0000 内的数,超过这个计算结果就可能不对了。
#17
风吹过b2025-03-20 15:50
因了超过 long 范围
超范围的整数计算,需要上 大数运算 ,
这个不要用VB6 ,因为没有现成的大数运算库。

#18
ysr28572025-03-21 07:53
回复 17楼 风吹过b
谢谢指导!我是用自己编辑的程序,速度太慢!
#19
ysr28572025-03-21 17:52
vb6.0咋无法设定Decimal类型?网上查询vb语言有这种数据类型呢
#20
ysr28572025-04-07 02:19
Private Sub Command1_Click()
'可以,10万内不到1分钟
Dim a As Long, b As Long, s As Long
Dim c As Double
Dim x As Double
Dim y As Double, js As String

Text2 = ""
a = Val(Text1)
t = Timer
DoEvents

For x = 1 To a
    y = Abs(x * x - 39697) / 53
    y = Sqr(y)
    If Int(y) = y Then       '得到是一个整数
      If y < a And x * x >= 39697 Then '在输入范围内
        '得到一个解
        s = s + 1
        js = js & "x=" & x & "  y=" & y & vbCrLf
      End If
    End If
Next x

If s > 0 Then
    Text2 = js & vbCrLf & Timer - t
  Else
    Text2 = "  wu  jie"
End If


End Sub

Private Sub Command2_Click()

Text1 = ""
Text2 = ""

End Sub
1