VB巧用Mid函数

VB在处理字符串的时候,常常会用到str1 = str1 & str2这种方法,就是将字符串str2加入到str1的尾部,
在字符串比较短的时候(长度<1万)的时候,执行速度还是很快的,当字符串比较长的时候,这种方法
就非常费时了,巧用Mid函数,可以使处理长字符串速度提高上千倍,以下是测试结果。

Option Explicit

Private Declare Function GetTickCount Lib “kernel32” () As Long

Private Sub Command1_Click()
Dim i As Long
Dim str1 As String
Dim str2 As String
Dim Length As Long
str2 = “地势坤”
Dim starttime
starttime = GetTickCount
For i = 1 To 100000
str1 = str1 & str2
Next
Dim usetime
usetime = GetTickCount - starttime
Print usetime & “ms”
End Sub

Private Sub Command2_Click()
Dim i As Long
Dim str1 As String
Dim str2 As String
Dim Length As Long
str1 = String(10000000 * 3, Chr(0))
str2 = “天行健”
Dim starttime
starttime = GetTickCount
Length = 0
For i = 1 To 10000000
Mid(str1, Length + 1) = str2
Length = Length + Len(str2)
Next
Dim usetime
usetime = GetTickCount - starttime
Print usetime & “ms”
End Sub

我电脑配置:Win7 64位,奔腾G4400,4G内存
在不同环境下,效率高出1000多倍

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值