VB.NETでDelegateとlambda式を試す
# 5年ぶりにMS系のプロジェクト参画中...
VS2010のプロファイラってチョー便利。(詳細はまた)
ただ、ちょっと工夫がいる事も。
関数ごとに処理時間や回数は集計されてしまうんだ...
ある処理でなんらかの理由で遅いことが分かっている場合、2回目以降の処理時間だけ計測したい!
...なんてとき
んで、おそらく匿名メソッドとしてコンパイル時に命名されるであろうDelegateとlambda式を試すw
元のコード
Public Class Form1 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click s1() End Sub Private Sub s1() ' 何がしの処理 Debug.Print("呼ばれたよ") End Sub End Class
Delegateしてみる
Public Class Form1 Private executed As Integer = 0 Private Delegate Sub DeleSub() Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim s As DeleSub = Nothing Select Case executed Case 0 s = Sub() s1() Case 1 s = Sub() s1() Case 2 s = Sub() s1() Case Else s = Sub() s1() End Select s() executed += 1 End Sub Private Sub s1() ' 何がしの処理 Debug.Print("呼ばれたよ") End Sub End Class
lambdaしてみる
Public Class Form1 Private executed As Integer = 0 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Select Case executed Case 0 Call Sub() s1() Case 1 Call Sub() s1() Case 2 Call Sub() s1() Case Else Call Sub() s1() End Select executed += 1 End Sub Private Sub s1() ' 何がしの処理 Debug.Print("呼ばれたよ") End Sub End Class
ちなみに、
_Lambda$__[n]
の連番は、コールツリーのソースファイルと行番号で確認したけど、
コンパイル時の出現順みたい。