Click here to Skip to main content
15,892,575 members
Articles / Programming Languages / MSIL

Dynamic Objects, Factories, and Runtime Machines to Boost Performance

Rate me:
Please Sign up or sign in to vote.
4.79/5 (10 votes)
27 Nov 2007CPOL7 min read 48.5K   303   46  
An examination of dynamic object instantiation and runtime machines to boost performance.
Imports Widgets
Imports System.Reflection
Module TestBench

	Private _Counter As New Counter

	Private _WidgetType As Type
	Private _WidgetTypeA As Type = GetWidgetType("Widgets.SuperWidget")
	Private _WidgetTypeB As Type = GetWidgetType("Widgets.WonderWidget")

	Private _Iterations As Integer = 1000000

	Sub Main()

		GC.Collect()
		GC.WaitForPendingFinalizers()

		' Run multiple passes as the first pass has some overhead from building the cache...
		For i As Integer = 1 To 2

			' Type selection would normally be driven by an external source, such as a config file.
			Select Case i
				Case 1
					_WidgetType = _WidgetTypeA
				Case 2
					_WidgetType = _WidgetTypeB
			End Select

			Console.WriteLine("Creating {0} {1}'s", _Iterations, _WidgetType.Name)
			Console.WriteLine("---------------------------------------------")
			Console.WriteLine("InstanceDirect		: {0}ms", InstanceDirect.ToString("0.000000"))
			Console.WriteLine("InstanceByFactory	: {0}ms", InstanceByFactory.ToString("0.000000"))
			Console.WriteLine("InstanceByDelegate	: {0}ms", InstanceByDelegate.ToString("0.000000"))
			Console.WriteLine("InstanceByCtorInfo	: {0}ms", InstanceByCtorInfo.ToString("0.000000"))
			Console.WriteLine("InstanceByActivator	: {0}ms", InstanceByActivator.ToString("0.000000"))
			Console.WriteLine("")
		Next

		Console.WriteLine("Done!")
		Console.ReadLine()

	End Sub

	Private Function InstanceDirect() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = New SuperWidget
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByFactory() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidget(_WidgetType)
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByCtorInfo() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidgetByConstructorInfo(_WidgetType)
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByActivator() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidgetBySystemActivator(_WidgetType)
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByDelegate() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidgetByDelegate(_WidgetType)
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

End Module

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) @Everywhere
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions