SharpDevelop Community

Get your problems solved!
Welcome to SharpDevelop Community Sign in | Join | Help
in Search

Optimization: Convert C# to VB.NET

Last post 12-24-2008 3:36 PM by LukeSw. 7 replies.
Page 1 of 1 (8 items)
Sort Posts: Previous Next
  • 11-13-2007 12:38 PM

    • Ewald
    • Not Ranked
    • Joined on 11-13-2007
    • Posts 2

    Optimization: Convert C# to VB.NET

    The following C# code 

       String Test = "My Test\n" ;

    converts to VB.NET

       Dim Test As String = "My Test" & Chr(10) & "" 

    and back to C#

       string Test = "My Test" + Strings.Chr(10) + ""; 

    I think the code could be optimized by replacing escape sequences in the converted VB.NET code with their constants like vbLF, vbNewline etc. and vice versa.

    Also the empty string can be either removed completely or replaced with the faster String.Empty.


    Filed under:
  • 11-13-2007 1:15 PM In reply to

    Re: Optimization: Convert C# to VB.NET

    string.Empty is not faster than "", if you look at the generated native code you'll see that both compile to a single MOV instruction. There are a lot of myths about the performance of string operations in .NET that are simply not true (especially common myth: "StringBuilder is faster than +").

    The converter should not create any empty string literals; and you're right that constants should be preferred when they exist (are there constants for \0, \v, \b, \f, \a ?)

    Daniel Grunwald
  • 11-13-2007 1:39 PM In reply to

    • Ewald
    • Not Ranked
    • Joined on 11-13-2007
    • Posts 2

    Re: Optimization: Convert C# to VB.NET

    You're certainly right with the string performance. I 'converted' that knowledge from VB6, got used to replace "" with vbNullString etc. and have not checked again with .NET.

    I knew of constants for Chr(8) up to Chr(13) and some combinations. Chr(0) is vbNullChar.

     

    Filed under:
  • 04-02-2008 4:46 AM In reply to

    Re: Optimization: Convert C# to VB.NET

     Implemented in revision 3022.

    Daniel Grunwald
  • 12-22-2008 5:05 PM In reply to

    • LukeSw
    • Not Ranked
    • Joined on 12-22-2008
    • Posts 2

    Re: Optimization: Convert C# to VB.NET

    DanielGrunwald:
    There are a lot of myths about the performance of string operations in .NET that are simply not true (especially common myth: "StringBuilder is faster than +").
    I'm sorry, but you're not correct. StringBuilder class is much faster than "+" operator for strings. A sample code posted here http://phpfi.com/390910 produces on my PC the following results:
    00:00:05.1243101
    00:00:00.0026769
    So as you can see StringBuilder is faster by 3 orders of magnitude.
    Łukasz
  • 12-22-2008 5:23 PM In reply to

    Re: Optimization: Convert C# to VB.NET

    A single StringBuilder is faster than 100000 + operator calls.

    But 100000 + operator calls are faster than using 100000 StringBuilders.

    If you're building one a single string inside a loop, StringBuilder is faster.

    If you're building many strings (where each individual string has a constant number of parts so you don't need a loop), the + operator is faster.

     

    So as you can see StringBuilder is faster by 3 orders of magnitude. 

    That's plainly incorrect. In your code, the StringBuilder version is O(n) while the + operator version is O(n^2). So the more iterations you do, the faster StringBuilder gets. It's not faster by any constant factor.

    But when used correctly, the + operator is faster than StringBuilder.

    Daniel Grunwald
  • 12-22-2008 5:33 PM In reply to

    Re: Optimization: Convert C# to VB.NET

     Here's my benchmark:

    http://phpfi.com/390926

     Results on my machine:

    + operator:     00:00:00.0354874
    StringBuilder: 00:00:00.1294351
    string.Format: 00:00:00.1854459

    As you can see, the + operator is faster by a factor of 3 to 4.

    Daniel Grunwald
  • 12-24-2008 3:36 PM In reply to

    • LukeSw
    • Not Ranked
    • Joined on 12-22-2008
    • Posts 2

    Re: Optimization: Convert C# to VB.NET

    DanielGrunwald:
    But when used correctly, the + operator is faster than StringBuilder.
    Right, when there are small numbers of concatenations, eg. less than 10, + operator is faster than StringBuilder, but otherwise StringBuilder is faster.

    Please don't call it a myth, because StringBuilder used in proper situations is much faster than string + operator (or rephrase your myth into “StringBuilder is always faster than +”, so it really would be a myth).


    Merry Christmas and Happy New Year  0x07D9 ;)

    Łukasz

Page 1 of 1 (8 items)
Powered by Community Server (Commercial Edition), by Telligent Systems
Don't contact us via this (fleischfalle@alphasierrapapa.com) email address.