String functions vs. XML literals... speed

  • Thread starter Thread starter Arthur Dent
  • Start date Start date
A

Arthur Dent

I was wondering, ... does anyone know off hand which is faster?

I've seen articles where people suggest the method of using XML literals to
build strings... something like:

Dim s As String = <s>This is some string, built on <%= Format(Now,
"MM/dd/yyyy HH:mm") %></s>.Value

and I'm just curious how this might compare in speed to using String.Format,
more like:

Dim s As String = String.Format("This is some string, built on {0}",
Format(Now, "MM/dd/yyyy HH:mm"))
 
Arthur said:
I was wondering, ... does anyone know off hand which is faster?

I've seen articles where people suggest the method of using XML literals
to build strings... something like:

Dim s As String = <s>This is some string, built on <%= Format(Now,
"MM/dd/yyyy HH:mm") %></s>.Value

I'm not sure if an xml literal will end up as a string or as an object
tree, but either way you are adding another layer that the code has to
work through to get to the result.
and I'm just curious how this might compare in speed to using
String.Format, more like:

Dim s As String = String.Format("This is some string, built on {0}",
Format(Now, "MM/dd/yyyy HH:mm"))

I don't see why you are using the Format function in a call to
String.Format. Use the String.Format method as it was intended:

Dim s As String = String.Format("This is some string, built on
{0:MM/dd/yyyy HH:mm}", Now)

If you only have a single value to format, you can do the same with an
overload of the ToString method:

Dim s As String = Now.ToString("'This is some string, built on
'MM/dd/yyyy HH:mm")

I believe the last one is the most efficient way to format a date into a
string.
 
Back
Top