4
7

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 5 years have passed since last update.

VBAで特定の文字が含まれているのかの確認方法の処理速度比較

Last updated at Posted at 2016-10-21

測定条件

  • 実行環境:Windows 10 Pro 64bit / Microsoft Excel 2016 64bit
  • 検索文字列:2文字目が"b"、残りが"a"からなる10万文字の文字列 "abaaa....a"
  • ループ回数:1万回
  • 時間測定方法:VBAのTimer関数を用いた簡易計測

測定結果

方法 時間(秒)
InStr関数 0.141
InStrRev関数 4.352
Like演算子 0.000
正規表現(事前バインド) 0.008
正規表現(実行時バインド) 0.141
Replace関数 1.563

使用したコード

Sub 特定の1文字検索速度ベンチマーク()
'要参照設定"Microsoft VBScript Regular Expression 5.5"
    
    '条件
        '検索文字列 この中から"b"を探す
        Dim tgtTxt As String
        Let tgtTxt = "ab" & VBA.String$(99998, "a")
        
        'ループ回数
        Const LOOP_CNT& = 10000
    
    
    '準備
        '事前バインディング正規表現
        Dim earlyBindRegExp As RegExp
        Set earlyBindRegExp = New RegExp
        Let earlyBindRegExp.Pattern = "b"
        
        '実行時バインディング正規表現
        Dim lateBindRegExp As Object
        Set lateBindRegExp = New RegExp
        Let lateBindRegExp.Pattern = "b"
        
        '計測とかループ用
        Dim st!, i&, result As Boolean
        Const MY_FORMAT$ = "0.000"
    
    '----------------------------------------------------------
    
    'Instr
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = (VBA.InStr(1, tgtTxt, "b") <> 0)
    Next i
    Debug.Print "InStr", VBA.Format$(Timer - st, MY_FORMAT)
    
    '----------------------------------------------------------
    
    'InstrRev
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = (VBA.InStrRev(tgtTxt, "b") <> 0)
    Next i
    Debug.Print "InStrRev", VBA.Format$(Timer - st, MY_FORMAT)
    
    '----------------------------------------------------------
    
    'Like演算子
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = (tgtTxt Like "*b*")
    Next i
    Debug.Print "Like", VBA.Format$(Timer - st, MY_FORMAT)
    
    '----------------------------------------------------------
    
    '事前バインディング正規表現
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = earlyBindRegExp.Test(tgtTxt)
    Next i
    Debug.Print "EarlyRegExp", VBA.Format$(Timer - st, MY_FORMAT)
    
    '----------------------------------------------------------
    
    '実行時バインディング正規表現
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = lateBindRegExp.Test(tgtTxt)
    Next i
    Debug.Print "LateRegExp", VBA.Format$(Timer - st, MY_FORMAT)
    
    '----------------------------------------------------------
    
    'Replace
    Let st = Timer
    For i = 1 To LOOP_CNT
        Let result = (VBA.Len(tgtTxt) <> VBA.Len(VBA.Replace(tgtTxt, "b", "")))
    Next i
    Debug.Print "Replace", VBA.Format$(Timer - st, MY_FORMAT)
   
End Sub

わかったこと

参照設定等の手間を考えるとLike演算子が扱いやすく、出来ることもそれなりに多いので優秀。
(ただしWin7 64bit Excel 2010 32bitの環境ではInStrの方が早かった記憶があるので環境によっては変化するかも)

InStr/InStrRev関数は該当文字が見つかった時点で処理を終了している。

4
7
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
4
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?