String functions vs. XML literals... speed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Arthur Dent

    String functions vs. XML literals... speed

    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"))

  • =?ISO-8859-1?Q?G=F6ran_Andersson?=

    #2
    Re: String functions vs. XML literals... speed

    Arthur Dent wrote:
    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.

    --
    Göran Andersson
    _____
    Göran Anderssons privata hemsida.

    Comment

    Working...