VB IF 的判断速度有多快?

vbtmd 2008-03-20 06:26:01
定义一个字节数组很大,是搜索的源数据Tby ,定义一个搜索的字节数组Sby
例如:

Private Declare Function GetTickCount Lib "kernel32" () As Long

Tby(1 To 524288, 1 To 1024) As Byte
Sby(1 to 1024) as Byte

Dim i as integer,y as integer,x as integer ,n as integer,usetime as integer

For i = 1 To 1024

Sby(i) = Tby(524288, i)

Next

DealTime = GetTickCount

For y = 1 To 524288

For x = 1 To 1024

If Sby(x) = Tby(y, x) Then

n = n + 1

Else

n = 0

Exit For

End If

Next

If n = 1024 Then

Usetime = GetTickCount - DealTime & "毫秒"

Exit For

End If

Next

才用30多毫秒? 太快了吧?
...全文
359 点赞 收藏 31
写回复
31 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
看起来VB6的编译器优化得不错呀.

IDE里貌似是老实地执行,而编译后可能对一些逻辑上无效的代码作了处理.
回复
29楼代码测试结果:

IDE中:

7490
16480
32650

编译后:

1406
1422
1406

如果你想速度快,使用汇编语言吧.应该没有比它更快的编程语言了,可以精确到CPU指令等级.

能达到什么样的效率,全看你水平了.
回复
xstong1982 2008-03-21
lz想测if速度的话可以这样
,延用28楼的代码


Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Sub Main()

Dim y As Long, x As Long, Dealtime As Long
Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
Next
Next
MsgBox GetTickCount - Dealtime & "毫秒"

Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
if x = y then
end if
Next
Next
MsgBox GetTickCount - Dealtime & "毫秒"

Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
if x = y then
end if
if x = y then
end if
Next
Next
MsgBox GetTickCount - Dealtime & "毫秒"

End Sub


回复
33184777 2008-03-20
盲目崇拜!
回复
njstalk 2008-03-20
把vb界面去掉,有人试过没有呢?

Option Explicit
Private Declare Function GetTickCount Lib "kernel32" () As Long
Sub Main()

Dim y As Long, x As Long, Dealtime As Long
Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
Next
Next
MsgBox GetTickCount - Dealtime & "毫秒"

End Sub

去掉界面后,在我的amd sempron 2g & 1g 内存上大概是860ms 左右。
回复
bluefox1979 2008-03-20
???????????
回复
bluefox1979 2008-03-20
????????

回复
forbearORfolie 2008-03-20
我说的是IF语句本身的执行速度
LZ说的好象是vb的IF比其他语言要快这个意思(不然,是什么意思呢?)
回复
vbtmd 2008-03-20
ReDim Tby(1 To 524288, 1 To 1024) As Byte 512M

编译后运行 没有溢出错误了
回复
forbearORfolie 2008-03-20
其实单从语句的执行速度上讲,我的计算大概花了0.8秒,输出花了其它花在输出上面了
C写的那个程序,只能看到效果,没办法测试。
就我的估计,如果跟C比,vb的语句执行速度起码要慢20倍以上,对系统对象操作更慢,起码要慢100倍以上。
回复
vbman2003 2008-03-20
forbearORfolie说的不准确吧,和IF有关的,你细细看一下,满足一定条件会exit for的
回复
forbearORfolie 2008-03-20
这个代码快,跟if判断无关,for有关,如果用do,while就会慢一些,另外,byte在内存中处理的速度是很快的。
不过这仍然不能说vb的代码快。
我做的一个彩票过滤软件,35选5,全组合324632注按照每行一注,两个号码之间有一个空格的格式,从实时计算到输出到richtextbox,全部用时才3秒,这在vb中应该算快的吧,可是人家c做出来的,2千多万注,从计算到输入,才0.89秒。
没法比啊。。。
回复
vbman2003 2008-03-20
我在一个1G内存的机器上测试
在这里 ReDim Tby(1 To 524288, 1 To 1024) As Byte 提示内存溢出
跑不动 :(
回复
jadeluo 2008-03-20
最坏的情况下, 需要524288*1024次循环。
回复
vbtmd 2008-03-20
好给你个完整代码:


Option Explicit

Private Declare Function timeGetTime Lib "winmm.dll" () As Long
Private Declare Function GetTickCount Lib "kernel32" () As Long

Private DealTime As Long

Private Sby(1 To 1024) As Byte
Private Tby() As Byte '

Private Sub Command1_Click()

Dim FileN As String
Dim OTime As Long
Dim i As integer

FileN = "D:\大百科\大不列颠百科\npebus08.iso" '一个用来测试的文件

Ot = GetTickCount


ReDim Tby(1 To 524288, 1 To 1024) As Byte

Open FileN For Binary As #1

Text1 = LOF(1)

Get #1, , Tby()

Close #1

Text2 = GetTickCount - OTime & "毫秒" '打开文件的时间

End Sub

Private Sub Command2_Click()

Dim x As Long, y As Long
Dim n As Long
Dim m As Long
Dim i As Integer

DealTime = GetTickCount



For i = 1 To 1024

Sby(i) = Tby(524288, i)

Next

For y = 1 To 524288

For x = 1 To 1024

If Sby(x) = Tby(y, x) Then

n = n + 1
m = m + 1

Else

n = 0

Exit For

End If

Next

If n = 1024 Then

Exit For

End If


Next

Text1 = m 'm 是应该代表实际循环的次数

Text3 = GetTickCount - DealTime & "毫秒"


End Sub
回复
vbman2003 2008-03-20
秀峰的代码在我机器上跑5422毫秒
看来我的机器该扔了,呵呵
回复
vbtmd 2008-03-20
Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim y As Long, x As Long, Dealtime As Long
Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
Next
Next
Debug.Print GetTickCount - Dealtime & "毫秒"
End
End Sub

'不会循环 y*x 次

回复
vbman2003 2008-03-20
[Quote=引用 10 楼 vbtmd 的回复:]
这个代码是用来说名 问题的 明白?

数组不为空
[/Quote]

代码要能说明问题,起码就要能给别人测试吧?
当然分析一下你的代码,是可以理解其意思的,但个人的理解方法不一样,理解的也不一定准确,你看上面已经有人误解你的意思了......
不知道你什么配置的机器,你的代码在我机器上跑不动......
回复
jadeluo 2008-03-20
变量类型申明成Integer是肯定不行了。30毫秒也是肯定不可能的。

不信,你可以试试下面的代码,没有任何IF,就空循环:


Private Declare Function GetTickCount Lib "kernel32" () As Long
Private Sub Form_Load()
Dim y As Long, x As Long, Dealtime As Long
Dealtime = GetTickCount
For y = 1 To 524288
For x = 1 To 1024
Next
Next
Debug.Print GetTickCount - Dealtime & "毫秒"
End
End Sub
回复
vbtmd 2008-03-20
不是空值.空值还对比啥啊?
回复
加载更多回复
相关推荐
发帖
VB基础类
创建于2007-09-28

7490

社区成员

VB 基础类
申请成为版主
帖子事件
创建了帖子
2008-03-20 06:26
社区公告
暂无公告