performance testing

R

Random

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))
reader = (load xml from file code here)
watch = Stopwatch.StartNew()
mycls = CType(ser.Deserialize(reader), MyClass)
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
 
M

Massimo

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?

Two things that immediately comes to mind are the JIT for the program itself
and the O.S. cache for the XML file.


Massimo
 
L

Larry Lard

Random said:
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:
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.
reader = (load xml from file code here)
watch = Stopwatch.StartNew()
mycls = CType(ser.Deserialize(reader), MyClass)

This isn't.
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top