0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

勉強メモ10_VBAの文字列結合を一瞬で終わらせるメモ(&結合からjoinに変える)

Last updated at Posted at 2020-12-20

★0 はじめに
  最近VBAの仕事で普通の文字列結合を
  「&」で結合したらばりばり遅いということを知り、
 「1234」という文字を100万回結合して時間を測定する実験を行った。

★1 &でつなげるとまじ遅い、以下の100万ループは30分かかった

Sub Macro1()
  
  Dim kaishi As Single
  kaishi = Timer

  Dim a As Long
  Dim sq As String
  
  For a = 1 To 1000000
    sq = sq & "1234"
  
  Next a
  
  MsgBox "処理にかかった時間は" & Round(Timer - kaishi, 1) & "秒です。"
  
End Sub

★2 JOINでやった場合、1秒で終わった。(midの方がもっと速いらしいけど)

Sub Macro1()
  
  Dim kaishi As Single
  kaishi = Timer
  
  Dim a As Long
  Dim ss() As String
  Dim sq As String
  
  For a = 1 To 1000000
  
   ReDim Preserve ss(a - 1)
   ss(a - 1) = "1234"
  
  Next a
  
  sq = Join(ss, "")
  
  MsgBox "処理にかかった時間は" & Round(Timer - kaishi, 1) & "秒です。"
  
End Sub

★3 参考資料

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?