Groups | Blog | Home
all groups > dotnet framework > january 2007 >

dotnet framework : performance testing


Random
1/31/2007 3:01:37 PM
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

Massimo
2/1/2007 9:14:48 AM
"Random" <cipherlad@hotmail.com> ha scritto nel messaggio
news:e$ROEvYRHHA.3444@TK2MSFTNGP03.phx.gbl...

[quoted text, click to view]

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


Massimo
Larry Lard
2/1/2007 11:41:50 AM
[quoted text, click to view]

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]

This isn't.

[quoted text, click to view]

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
AddThis Social Bookmark Button