Performance Difference between HttpContext user and Thread user

Posted by atrueresistance on Stack Overflow See other posts from Stack Overflow or by atrueresistance
Published on 2012-12-18T17:02:07Z Indexed on 2012/12/18 17:02 UTC
Read the original article Hit count: 306

I am wondering what the difference between HttpContext.Current.User.Identity.Name.ToString.ToLower and Thread.CurrentPrincipal.Identity.Name.ToString.ToLower. Both methods grab the username in my asp.net 3.5 web service. I decided to figure out if there was any difference in performance using a little program. Running from full Stop to Start Debugging in every run.

Dim st As DateTime = DateAndTime.Now
        Try
            'user = HttpContext.Current.User.Identity.Name.ToString.ToLower
            user = Thread.CurrentPrincipal.Identity.Name.ToString.ToLower
            Dim dif As TimeSpan = Now.Subtract(st)
            Dim break As String = "nothing"

        Catch ex As Exception
            user = "Undefined"
        End Try

I set a breakpoint on break to read the value of dif. The results were the same for both methods.

    dif.Milliseconds    0   Integer

    dif.Ticks           0   Long

Using a longer duration, loop 5,000 times results in these figures.

Thread Method

        run 1
    dif.Milliseconds    125     Integer
    dif.Ticks           1250000 Long

        run 2 
    dif.Milliseconds    0   Integer
    dif.Ticks   0   Long

        run 3 
    dif.Milliseconds    0   Integer
    dif.Ticks   0   Long

HttpContext Method

        run 1
    dif.Milliseconds    15  Integer
    dif.Ticks   156250  Long

        run 2
    dif.Milliseconds    156 Integer
    dif.Ticks   1562500 Long

        run 3
    dif.Milliseconds    0   Integer
    dif.Ticks   0   Long

So I guess what is more prefered, or more compliant with webservice standards? If there is some type of a performance advantage, I can't really tell. Which one scales to larger environments easier?

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about vb.net