Click here to Skip to main content
12,404,478 members (75,889 online)
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads

Stats

31.2K views
256 downloads
46 bookmarked
Posted

Dynamic Objects, Factories, and Runtime Machines to Boost Performance

, 27 Nov 2007 CPOL
An examination of dynamic object instantiation and runtime machines to boost performance.
PerformanceTestConsole
bin
obj
Debug
temp
TempPE
PerformanceTestConsole.vbproj.user
TestConsole.suo
Widgets
bin
obj
Debug
temp
TempPE
Widgets.vbproj.user
Widgets
DynamicFactoryDemo.suo
PerformanceTestConsole.exe
Widgets.dll
PerformanceTestConsole.vbproj.user
Widgets.dll
Widgets.vbproj.user
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)

Share

About the Author

Philip Liebscher
Technical Lead Interactive Data Corporation
United States United States
No Biography provided

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.160721.1 | Last Updated 27 Nov 2007
Article Copyright 2007 by Philip Liebscher
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid