Click here to Skip to main content
12,450,347 members (44,345 online)
Click here to Skip to main content
Articles » Languages » VB.NET » General » Downloads

Stats

31.4K views
257 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
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

You may also be interested in...

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