locked
Which has better performance? RRS feed

  • Question

  • User-77030870 posted

    I could not find a forum that was dedicated to code performance but perhaps there should be one?  If there is one can someone point me to it? 

    So here is my question:

    I am parsing out text links in one of my applications and it goes though several in a loop.

    ary() is an array of links as strings. 

    Here is an example link:

     

    <a name='137' href='mailto:xxxx@robsonforensic.com'>Jim Orosz</a> with <a name='25'href='Energy.aspx'>Energy</a>

     The object is to pull out the ID numbers, where name='137' and also the 25

    So which is faster this:

    links = ary(i).Substring(ary(i).IndexOf("name='") + 6, ary(i).IndexOf("' href") - 2)
    ExpertId = links.Substring(0, links.IndexOf("'"))
    Which truncates off the beginning first, and then you just know to stop at the next single apostrophe to get the number.
    Or the following one liner:
     
    links = ary(i).Substring(ary(i).IndexOf("name='") + 6, -(ary(i).IndexOf("name='") + 6 - ary(i).IndexOf("' href"))
     

     More complicated but does the same exact thing.

     

    Wednesday, May 20, 2009 7:47 PM

Answers

  • User1006193418 posted

    Hi Coty,

    Why not have a try with Regular Expression? Here is a simple demo I made for you based on the string you gave and it returns me 137 and 25 as the results. 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        Dim s As String = "&lt;a name='137' href='mailto:xxxx@robsonforensic.com'&gt;Jim Orosz</a> with <a name='25'href='Energy.aspx'>Energy</a>"
        Dim ms As MatchCollection = Regex.Matches(s, "name=['|""](\d+)['|""]")
    
        For Each m As Match In ms
            Response.Write(m.Groups(1).ToString() & "<br />")
        Next
    
    End Sub

    For some more information about Regular Expression,  you can refer to MSDN library at these links:

    Regular Expression Syntax: http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx

    Regular Expressions in ASP.NET: http://msdn.microsoft.com/en-us/library/ms972966.aspx

    More examples on RegEx: http://msdn.microsoft.com/en-us/library/kweb790z.aspx

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2009 2:35 AM

All replies

  • User-1659704165 posted

     Hi,

    It is Better To use Namevalue Collection ..

    http://www.developmentnow.com/g/36_2005_7_0_0_555476/NameValueCollection-QueryString.htm

    http://blog.leekelleher.com/tag/c/

    http://dotnetperls.com/Content/QueryString-NET.aspx

    http://msdn.microsoft.com/en-us/library/system.web.httprequest.querystring.aspx

    Thursday, May 21, 2009 1:32 AM
  • User-77030870 posted

    I don't think that works for me, the string I am trying to parse is directly from a database and not a URL.

    This is one of those strings stored in a single cell:

    "<a name='137' href='mailto:xxxx@robsonforensic.com'>Jim Orosz</a> with <a name='25'href='Energy.aspx'>Energy</a>"

     I don't think this can be converted to a NameValueCollection without parsing out the name\values.  Unless I am missing something from your examples...let me know.

     

    Coty

    Thursday, May 21, 2009 7:53 AM
  • User1006193418 posted

    Hi Coty,

    Why not have a try with Regular Expression? Here is a simple demo I made for you based on the string you gave and it returns me 137 and 25 as the results. 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
        Dim s As String = "&lt;a name='137' href='mailto:xxxx@robsonforensic.com'&gt;Jim Orosz</a> with <a name='25'href='Energy.aspx'>Energy</a>"
        Dim ms As MatchCollection = Regex.Matches(s, "name=['|""](\d+)['|""]")
    
        For Each m As Match In ms
            Response.Write(m.Groups(1).ToString() & "<br />")
        Next
    
    End Sub

    For some more information about Regular Expression,  you can refer to MSDN library at these links:

    Regular Expression Syntax: http://msdn.microsoft.com/en-us/library/1400241x(VS.85).aspx

    Regular Expressions in ASP.NET: http://msdn.microsoft.com/en-us/library/ms972966.aspx

    More examples on RegEx: http://msdn.microsoft.com/en-us/library/kweb790z.aspx

    Best Regards,
    Shengqing Yang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, May 26, 2009 2:35 AM