Coffeehouse Thread

14 posts

.NET can be fast - Today I was surprised

Back to Forum: Coffeehouse
  • User profile image
    littleguru

    Hi C9 fellows,

    today I was very very surprised and I'm still excited. .NET can be really fast, if you get it right. Today I wrote a Tokenizer for a parser I'm going to write.
    The tokenizer has quite a few rules he is following. The class is like 350 lines of pure code.
    I created a short demo application to test it.

    for (int i = 0; i < 30; i++)
    {
    Tokenizer tokenizer = new Tokenizer("(Upper(SubString(UserName, 1, 3)) = {0} And " +
    "Call[Duration > \"]blah\"]) Or UserName = 'Christian' SortBy UserName Asc");
    DateTime now = DateTime.Now;

    foreach (Token token in tokenizer)
    {
      tokens.Add(token);
    }

    DateTime then = DateTime.Now;
    Console.WriteLine("Time #" + i + " in Milliseconds: " + ((TimeSpan)(then - now)).TotalMilliseconds + " ms");
    }

    As you see the string that is analyzed is quite complicated (see the constructor call).
    Look at the output on the console:

    Time #0 in Milliseconds: 10,0144 ms
    Time #1 in Milliseconds: 0 ms
    Time #2 in Milliseconds: 0 ms
    Time #3 in Milliseconds: 0 ms
    Time #4 in Milliseconds: 0 ms
    Time #5 in Milliseconds: 0 ms
    Time #6 in Milliseconds: 0 ms
    Time #7 in Milliseconds: 0 ms
    Time #8 in Milliseconds: 0 ms
    Time #9 in Milliseconds: 0 ms
    Time #10 in Milliseconds: 0 ms
    Time #11 in Milliseconds: 0 ms
    Time #12 in Milliseconds: 0 ms
    Time #13 in Milliseconds: 0 ms
    Time #14 in Milliseconds: 0 ms
    Time #15 in Milliseconds: 0 ms
    Time #16 in Milliseconds: 0 ms
    Time #17 in Milliseconds: 0 ms
    Time #18 in Milliseconds: 0 ms
    Time #19 in Milliseconds: 0 ms
    Time #20 in Milliseconds: 0 ms
    Time #21 in Milliseconds: 0 ms
    Time #22 in Milliseconds: 0 ms
    Time #23 in Milliseconds: 0 ms
    Time #24 in Milliseconds: 0 ms
    Time #25 in Milliseconds: 0 ms
    Time #26 in Milliseconds: 0 ms
    Time #27 in Milliseconds: 0 ms
    Time #28 in Milliseconds: 0 ms
    Time #29 in Milliseconds: 0 ms


    As you see the first run takes 10 milliseconds. After that (I do no caching) the "tokenizing" takes 0 milliseconds. It still takes some processor time, but I guess 10 milliseconds is the lowest unit you can measure with my technique.

    I'm still surprised. Tumbs up for .NET!!!

  • User profile image
    Ovidiu.​Platon

    If you want better performance figures, you might be interested in implementing a performance counter. Take a look in the System.Diagnostics namespace, you'll find everything you need in there.

    Off-topic: I already knew that .NET rules, both in terms of speed, and of memory footprint. I fired up NetBeans 4.0 today and it takes about 70 MB of RAM just to boot up. Compare that to about 15-20 MB used by Visual Studio. After editing a simple Hello, World form (one label, one button) and compiling the project, NetBeans reaches 140 MB of RAM (that darn GC is very lazy in Java). Try doing it in VS.NET and measure Smiley

  • User profile image
    rhm

    The sample framework for the latest hit of the DirectX9 Managed framework includes some code to do timing that uses PInvoke to access the QueryPerformanceCounter API. That might be more useful for benchmarking. Of course there's always the old fallback of running the test 1000 times, measuring the duration of that and then dividing by 1000.

  • User profile image
    Marsella

    Yeah, I think .Net is as fast as C++ once it's over the initial jit compilation on the first run of the code.

  • User profile image
    littleguru

    Marsella wrote:
    Yeah, I think .Net is as fast as C++ once it's over the initial jit compilation on the first run of the code.


    I knew it is fast and I wrote quite a lot code in .NET until now. I read a lot books and know about the GC, JIT, GAC, bla bla, ... But I'm always simply surprised how the Microsoft guys made it that fast!

    Today was again such an Aha/Wow moment.

    PS.: The code generates 350 tokens (each one a class). After the 30 runs.

  • User profile image
    littleguru

    rhm wrote:
    Of course there's always the old fallback of running the test 1000 times, measuring the duration of that and then dividing by 1000.


    Running it 1000 times (with my old time measure method) and dividing the result by 1000 got me:

    0,050072 ms

    *big smile all over the face*

  • User profile image
    Rossj

    littleguru wrote:
    rhm wrote:Of course there's always the old fallback of running the test 1000 times, measuring the duration of that and then dividing by 1000.


    Running it 1000 times (with my old time measure method) and dividing the result by 1000 got me:

    0,050072 ms

    *big smile all over the face*


    I'd be interested to see your results if you had 1000 different strings parsed instead of the same string 1000 times.

  • User profile image
    littleguru

    Rossj wrote:
    I'd be interested to see your results if you had 1000 different strings parsed instead of the same string 1000 times.


    It does not matter which string is parsed. I recreate each time the tokenizer object. The new object does nothing know about the old one.

  • User profile image
    Rossj

    littleguru wrote:
    Rossj wrote:I'd be interested to see your results if you had 1000 different strings parsed instead of the same string 1000 times.


    It does not matter which string is parsed. I recreate each time the tokenizer object. The new object does nothing know about the old one.


    And you are confident the compiler does not see the same MSIL being generated 30 times and optimise it in a way that would not be realistic in real world usage?

    If you're confident of that and think your tests are adequate then I guess thats okay Wink  I don't disagree about the performance of .Net, it *is* impressive.

  • User profile image
    Senkwe Chanda

    >>Off-topic: I already knew that .NET rules, both in terms of speed, and of memory footprint. I fired up NetBeans 4.0 today and it takes about 70 MB of RAM just to boot up. Compare that to about 15-20 MB used by Visual Studio. After editing a simple Hello, World form (one label, one button) and compiling the project, NetBeans reaches 140 MB of RAM (that darn GC is very lazy in Java). Try doing it in VS.NET and measure :<<

    VS.Net is a C++ application.

  • User profile image
    prog_dotnet

    you should give it a try with stringbuilder...

    In this; http://www.dotnetrocks.com/default.aspx?showID=88, episode of .NET Rocks, Jay Roxe( Product Manager for Visual Basic at Microsoft) talk about his work on the .NET Framework including System.Object, System.String, and System.Text.StringBuilder.

    you can also pick up the transcript and scroll down to page 7 were he talks about stringbuilder/string..

  • User profile image
    Ovidiu.​Platon

    Senkwe Chanda wrote:
    VS.Net is a C++ application.


    The host is unmanaged, but there are plenty of managed plugins in there. Have you ever thought how it's possible to have WYSIWYG Windows Forms/ASP.NET/Web Service/component designers? Or how come you write a visual component and test it in Visual Studio? (And the example list is long)

  • User profile image
    rhm

    It is a really odd mix of managed code and native COM based code. The VSIP docs have all the gory details.

  • User profile image
    iStation

    Marsella wrote:
    Yeah, I think .Net is as fast as C++ once it's over the initial jit compilation on the first run of the code.

    In coming multi-core proccesor era, a core will play a role of jitting automatically!
    I guess.
    Smiley

Comments closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums, or Contact Us and let us know.