Help us understand the problem. What is going on with this article?

【そんなもん使うな】Scripting.Dictionaryはくそ遅いという話

経緯

とある業務アプリケーションが遅すぎると現場で話題に。
原因を調査した結果、「Scripting.Dictionary」が諸悪の根源だった。

検証

COMコンポーネントの「Scripting.Dictionary」と.NETアセンブリの「Generic.Dictionary」の実行速度を比較してみた。
書き込み50000回を10セット実行する。

Dim s As Date
Dim ts1 As New List(Of TimeSpan)
Dim ts2 As New List(Of TimeSpan)
Dim i_ = 50000
Dim j_ = 10

For j = 0 To j_ - 1
    s = Now
    Dim com As New Scripting.Dictionary()
    For i As Object = 0 To i_ - 1
        com.Item(i) = i
    Next
    ts1.Add(Now - s)
Next

For j = 0 To j_ - 1
    s = Now
    Dim gen As New Generic.Dictionary(Of Object, Object)
    For i As Object = 0 To i_ - 1
        gen.Item(i) = i
    Next
    ts2.Add(Now - s)
Next

Debug.WriteLine(String.Join(vbCrLf, ts1.Select(Function(x) x.ToString()).ToArray()))
Debug.WriteLine((New Date(ts1.Select(Function(x) x.Ticks).Average())).ToString("HH:mm:ss.fff"))
Debug.WriteLine("")
Debug.WriteLine(String.Join(vbCrLf, ts2.Select(Function(x) x.ToString()).ToArray()))
Debug.WriteLine((New Date(ts2.Select(Function(x) x.Ticks).Average())).ToString("HH:mm:ss.fff"))
Debug.WriteLine("")

結果

およそ370倍ほどパフォーマンスに差があった。

Scripting.Dictionary実行時間
00:00:02.2260328
00:00:02.2419714
00:00:02.2410784
00:00:02.1870029
00:00:02.2119736
00:00:02.2210343
00:00:02.2079682
00:00:02.1930362
00:00:02.2549694
00:00:02.2000345

Scripting.Dictionary実行時間平均
00:00:02.218

Generic.Dictionary実行時間
00:00:00.0050092
00:00:00.0079916
00:00:00.0049991
00:00:00.0070003
00:00:00.0079994
00:00:00.0080074
00:00:00.0049925
00:00:00.0070002
00:00:00.0049995
00:00:00.0070002

Generic.Dictionary実行時間平均
00:00:00.006

結論

Scripting.Dictionaryなんか使うな
(言われなくても使わないだろうけど...)

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away