投稿日 | : 2003/02/25(Tue) 09:58 |
投稿者 | : 魔界の仮面弁士 |
Eメール | : |
URL | : |
タイトル | : Re: 便乗質問、&演算子を使用しない方法 |
00〜FFの範囲にあるランダムな16進数文字列を、3000個繋げるサンプルです。
RichTextBoxを1個、CommandButtonを4個貼って、それぞれの動作時間を比較して見てください。
Option Explicit
Private Const MAX_COUNT As Long = 4000&
Private Sub Command1_Click()
Dim L As Long
Dim T As Single
RichTextBox1.Text = ""
MsgBox "Textプロパティによる連結(その1)", vbInformation
T = Timer
With RichTextBox1
.Visible = False
For L = 1 To MAX_COUNT
.Text = .Text & toHex(Int(Rnd * 255))
Next
.Visible = True
End With
MsgBox Format$(Timer - T, "0.000") & "秒かかりました。", vbInformation, "Text(ループ内)"
End Sub
Private Sub Command2_Click()
Dim L As Long
Dim S As String
Dim T As Single
RichTextBox1.Text = ""
MsgBox "Textプロパティによる連結(その2)", vbInformation
T = Timer
With RichTextBox1
.Visible = False
S = ""
For L = 1 To MAX_COUNT
S = S & toHex(Int(Rnd * 255))
Next
.Text = S
.Visible = True
End With
MsgBox Format$(Timer - T, "0.000") & "秒かかりました。", vbInformation, "Text(ループ外)"
End Sub
Private Sub Command3_Click()
Dim L As Long
Dim T As Single
RichTextBox1.Text = ""
MsgBox "SelTextプロパティによる連結", vbInformation
T = Timer
With RichTextBox1
.Visible = False
For L = 1 To MAX_COUNT
.SelStart = (MAX_COUNT - 1) * 2
.SelText = toHex(Int(Rnd * 255))
Next
.Visible = True
End With
MsgBox Format$(Timer - T, "0.000") & "秒かかりました。", vbInformation, "SelText"
End Sub
Private Sub Command4_Click()
Dim L As Long
Dim O As Object
Dim T As Single
RichTextBox1.Text = ""
MsgBox "VB6のJoin関数による連結", vbInformation
T = Timer
With RichTextBox1
.Visible = False
Set O = CreateObject("Scripting.Dictionary")
For L = 1 To MAX_COUNT
O(L) = toHex(Int(Rnd * 255))
Next
.Text = Join(O.Items(), "")
Set O = Nothing
.Visible = True
End With
MsgBox Format$(Timer - T, "0.000") & "秒かかりました。", vbInformation, "Join関数"
End Sub
Private Function toHex(ByVal B As Byte)
If B < &H10 Then
toHex = "0" & Hex(B)
Else
toHex = Hex(B)
End If
End Function