Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads
 
Add your own
alternative version

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.
dynamicfactorydemo.zip
PerformanceTestConsole
bin
obj
Debug
temp
TempPE
PerformanceTestConsole.vbproj.user
TestConsole.suo
Widgets
bin
obj
Debug
temp
TempPE
Widgets.vbproj.user
Widgets
DynamicFactoryDemo.suo
runtimeobjectsdemo.zip
PerformanceTestConsole.exe
Widgets.dll
PerformanceTestConsole.vbproj.user
Widgets.dll
Widgets.vbproj.user
Imports Widgets
Imports System.Reflection
Imports System.Runtime.Serialization

Module TestBench

	Private _Counter As New Counter

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

	Private _Iterations As Integer = 1000000

	Sub Main()

		Dim TestNoCtor As TestNoCtor = DirectCast(FormatterServices.GetUninitializedObject(GetType(TestNoCtor)), TestNoCtor)

		GC.Collect()
		GC.WaitForPendingFinalizers()

		WidgetFactory.Init()

		_WidgetType = _WidgetTypeA
		Run()

		_WidgetType = _WidgetTypeB
		Run()

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

	End Sub

	Sub Run()
		Console.WriteLine("Creating {0} {1}'s", _Iterations, _WidgetType.Name)
		Console.WriteLine("---------------------------------------------")

		Console.WriteLine("Direct			: {0}ms", InstanceDirect.ToString("0.000000"))

		Console.WriteLine("FormatterServices	: {0}ms", InstanceByFormatterServices.ToString("0.000000"))

		Dim FactoryTime As Double = InstanceByFactory()
		Console.WriteLine("Factory			: {0}ms", FactoryTime.ToString("0.000000"))

		Dim FactoryCacheTime As Double = InstanceByFactoryCache()
		Console.WriteLine("Factory (Cached)	: {0}ms", FactoryCacheTime.ToString("0.000000"))

		Console.WriteLine("Delegate (Cached)	: {0}ms", InstanceByDelegate.ToString("0.000000"))

		Console.WriteLine("CtorInfo		: {0}ms", InstanceByCtorInfo.ToString("0.000000"))

		Console.WriteLine("CtorInfo (Cached)	: {0}ms", InstanceByCtorInfoCached.ToString("0.000000"))

		Console.WriteLine("Activator		: {0}ms", InstanceByActivator.ToString("0.000000"))

		Console.WriteLine("")
		Console.WriteLine("Cache Lookup Overhead	: {0}ms", (FactoryCacheTime - FactoryTime).ToString("0.000000"))
		Console.WriteLine("")
	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

	' System.Runtime.Serialization
	Private Function InstanceByFormatterServices() As Double

		Dim Widget As Widget = DirectCast(FormatterServices.GetUninitializedObject(_WidgetType), Widget)

		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = Widget.GetInstance()
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByFactoryCache() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidgetFromMachineCache(_WidgetType)
		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.GetWidgetFromMachine(_WidgetType)
		Next
		_Counter.Stop()
		Return _Counter.Seconds
	End Function

	Private Function InstanceByCtorInfoCached() As Double
		Dim Widget As Widget
		_Counter.Start()
		For i As Integer = 0 To _Iterations
			Widget = WidgetFactory.GetWidgetByConstructorInfoCached(_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

Public Class TestNoCtor

	Private Sub New()
	End Sub

	Public Sub New(ByVal a As String)
		IsInitialized = True
	End Sub

	Public IsInitialized As Boolean

End Class

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
Follow on   Google+

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