Click here to Skip to main content
15,895,746 members
Articles / Web Development / ASP.NET

Dynamic... But Fast: The Tale of Three Monkeys, A Wolf and the DynamicMethod and ILGenerator Classes

Rate me:
Please Sign up or sign in to vote.
4.94/5 (272 votes)
12 Jun 2012BSD6 min read 1M   1.2K   384  
How to use the DynamicMethod and ILGenerator classes to create dynamic code at runtime that outperforms Reflection.
Imports System.Collections.Generic
Imports System.Text
Imports System.Data.SqlClient
Imports System.Data
Imports System.Reflection
Imports System.Reflection.Emit

Module Program

    Sub Main()
        Dim connection As SqlConnection = New SqlConnection("Data Source=localhost; Initial Catalog=SpikeDB;Integrated Security=True")
        Dim command As SqlCommand = New SqlCommand("Select * From LoadSpike2", connection)

        connection.Open()

        LoadManually(command)
        LoadUsingReflection(command)
        LoadUsingDynamicCode(command)

        connection.Close()
        Console.WriteLine("Done!")
        Console.Read()
    End Sub

    Sub LoadManually(ByVal command As SqlCommand)
        Dim start As DateTime = DateTime.Now
        Console.WriteLine("Start Manual Load: {0}", start)

        Dim reader As SqlDataReader = command.ExecuteReader()
        Dim builder As ManualBuilder = New ManualBuilder()

        While reader.Read()
            Dim person As Person = builder.Build(reader)
        End While

        reader.Close()

        Dim done As DateTime = DateTime.Now
        Console.WriteLine("Stop Manual Load: {0}", done)
        Console.WriteLine("Elapsed Time: {0}", done - start)
        Console.WriteLine()
    End Sub

    Sub LoadUsingReflection(ByVal command As SqlCommand)
        Dim start As DateTime = DateTime.Now
        Console.WriteLine("Start Reflection Load: {0}", start)

        Dim reader As SqlDataReader = command.ExecuteReader()
        Dim builder As ReflectionBuilder(Of Person) = ReflectionBuilder(Of Person).CreateBuilder(reader)

        While reader.Read()
            Dim person As Person = builder.Build(reader)
        End While

        reader.Close()

        Dim done As DateTime = DateTime.Now
        Console.WriteLine("Stop Reflection Load: {0}", done)
        Console.WriteLine("Elapsed Time: {0}", done - start)
        Console.WriteLine()
    End Sub

    Sub LoadUsingDynamicCode(ByVal command As SqlCommand)
        Dim start As DateTime = DateTime.Now
        Console.WriteLine("Start Dynamic Load: {0}", start)

        Dim reader As SqlDataReader = command.ExecuteReader()
        Dim builder As DynamicBuilder(Of Person) = DynamicBuilder(Of Person).CreateBuilder(reader)

        While reader.Read()
            Dim person As Person = builder.Build(reader)
        End While

        reader.Close()

        Dim done As DateTime = DateTime.Now
        Console.WriteLine("Stop Dynamic Load: {0}", done)
        Console.WriteLine("Elapsed Time: {0}", done - start)
        Console.WriteLine()
    End Sub

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 BSD License


Written By
Software Developer (Senior) Scratch Audio
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