[quoted text, click to view] Random wrote:
> I have a class I'm using to load and deserialize an xml file. Within the
> class I'm using the Stopwatch class to time the deserialization. The first
> time through the process takes somewhere in the realm of 4900 milliseconds.
> If I repeatedly call the method that does the deserialization, the time
> drops a thousandfold to just 4 milliseconds.
>
> What's the reason for this? Code (minus the xml file) is below.
>
> Imports System.Xml
> Imports System.Xml.Serialization
> Imports System.Diagnostics
>
> Public Class GetMyClass() As MyClass
> Dim reader As XmlReader
> Dim mycls As MyClass
> Dim watch As Stopwatch
>
> Dim ser As XmlSerializer = New XmlSerializer(GetType(MyClass))
This is an expensive operation. From the docs:
[quoted text, click to view] >>
The XmlSerializer creates C# files and compiles them into .dll files to
perform this serialization. In .NET Framework 2.0, the XML Serializer
Generator Tool (Sgen.exe) is designed to generate these serialization
assemblies in advance to be deployed with your application, and improve
startup performance.
[quoted text, click to view] >>
> reader = (load xml from file code here)
> watch = Stopwatch.StartNew()
> mycls = CType(ser.Deserialize(reader), MyClass)
This isn't.
[quoted text, click to view] > watch.Stop()
> Console.WriteLine(String.Format("Deserialization of Xml took {0}
> milliseconds", watch.ElapsedMilliseconds()))
> Console.WriteLine(String.Format("Deserialization of Xml took {0} ticks",
> watch.ElapsedTicks()))
>
> reader.Close()
> Return mycls
> End Class
>
>
Every time you create a new XmlSerializer object, the type to be
serialized is examined, C# source is written to a temporary directory,
compiled, and an assembly from the resulting DLL is loaded. This is
obviously something you want to avoid doing more than often. Pre-2.0,
all you could do was just create one XmlSerializer for each relevant
type, then keep it hanging around. In 2.0, you can use SGen to create
the serialization DLL before run time.
--
Larry Lard
larrylard@googlemail.com
The address is real, but unread - please reply to the group