Results 1 to 9 of 9

Thread: C# and performance with cast and hashtable

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Qt products
    Qt3
    Platforms
    Windows
    Thanks
    53

    Default C# and performance with cast and hashtable

    What's better, please?
    Qt Code:
    1. void myFunction() {
    2. calc ( _listNode[0].MyTable["Name"].ToString() );
    3. print( _listNode[0].MyTable["Name"].ToString() );
    4. SomeOtherOperation onThatIstance( _listNode[0].MyTable["Name"].ToString() );
    5. }
    To copy to clipboard, switch view to plain text mode 
    Qt Code:
    1. void myFunction() {
    2. string s = _listNode[0].MyTable["Name"].ToString() ;
    3. calc (s);
    4. print(s);
    5. SomeOtherOperation onThatIstance(s);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Regards

  2. #2
    Join Date
    Aug 2007
    Location
    Gorakhpur, India
    Posts
    254
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11
    Thanks
    8
    Thanked 14 Times in 14 Posts

    Default Re: C# and performance with cast and hashtable

    Qt Code:
    1. void myFunction() {
    2. string s = _listNode[0].MyTable["Name"].ToString() ;
    3. calc (s);
    4. print(s);
    5. SomeOtherOperation onThatIstance(s);
    6. }
    To copy to clipboard, switch view to plain text mode 
    In above code there is a syntactical mistake. Here should be a toString() in spite of ToString. The above code is more suitable because first we should hold the variable and then process upon it. Otherwise repeatiation of same calculation exists.
    Anurag Shukla
    A man who never makes mistake is the man who never does anything! Theodre Rosvelt!

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C# and performance with cast and hashtable

    I don't know how C# handles that, but C++ compiler should be able to optimize the first block to look like the second one, so the performance should be equal.

  4. #4
    Join Date
    Oct 2006
    Posts
    279
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 40 Times in 39 Posts

    Default Re: C# and performance with cast and hashtable

    Quote Originally Posted by wysota View Post
    I don't know how C# handles that, but C++ compiler should be able to optimize the first block to look like the second one, so the performance should be equal.
    Why so? How should the compiler know that MyTable["Name"] always returns the same value?
    Last edited by spud; 24th January 2008 at 10:35. Reason: typo

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C# and performance with cast and hashtable

    Quote Originally Posted by spud View Post
    How should the compiler know that MyTable["Name"] always returns the same value?
    I don't know... heuristics? analysis? For methods such as "toString()" it should be easily achievable by looking at what it actually does.

  6. #6
    Join Date
    Oct 2006
    Posts
    279
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 40 Times in 39 Posts

    Default Re: C# and performance with cast and hashtable

    I don't know. What if the function was called randomString(), or currentTime()? We'd be in serious trouble if the compiler started making such assumptions.

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C# and performance with cast and hashtable

    Quote Originally Posted by spud View Post
    I don't know. What if the function was called randomString(), or currentTime()?
    At some point you'd encounter a volatile or non-const statement. You can trace if an object is modified or what is assigned where and if the assignment has a chance to mutate an object or not. I don't know if such checks are made but I don't see a reason not to do them (provided they are not more expensive than the gain). Consider the following code:

    Qt Code:
    1. class A {
    2. public:
    3. A(int x, int y) : _x(x), _y(y){}
    4. String toString() const { return String(_x)+"x"+String(_y); }
    5. private:
    6. int _x, _y;
    7. }
    To copy to clipboard, switch view to plain text mode 

    By looking at the constructor you see that _x and _y are nonvolatile ints that are not changed by calling toString() (because the method is const) and by inspecting the String class you might notice its constructor performs the same steps given the same argument and that concatenation of String objects is stationary (in terms of time) as well. etc. Compilers are getting smarter each day. They can let you know about uninitialized values or reading/writing out of bounds, then why not this?

  8. #8
    Join Date
    Oct 2006
    Posts
    279
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    6
    Thanked 40 Times in 39 Posts

    Default Re: C# and performance with cast and hashtable

    Quote Originally Posted by wysota View Post
    At some point you'd encounter a volatile or non-const statement. You can trace if an object is modified or what is assigned where and if the assignment has a chance to mutate an object or not. I don't know if such checks are made but I don't see a reason not to do them (provided they are not more expensive than the gain). Consider the following code:

    Qt Code:
    1. class A {
    2. public:
    3. A(int x, int y) : _x(x), _y(y){}
    4. String toString() const { return String(_x)+"x"+String(_y); }
    5. private:
    6. int _x, _y;
    7. }
    To copy to clipboard, switch view to plain text mode 

    By looking at the constructor you see that _x and _y are nonvolatile ints that are not changed by calling toString() (because the method is const) and by inspecting the String class you might notice its constructor performs the same steps given the same argument and that concatenation of String objects is stationary (in terms of time) as well. etc. Compilers are getting smarter each day. They can let you know about uninitialized values or reading/writing out of bounds, then why not this?
    Well if the functions are in different compilation units and the code isn't inlined, then the compiler doesn't have access to that information.

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C# and performance with cast and hashtable

    That's not entirely true. Some compilers (for example MSVC) can do optimizations across compilation units. And seriously there is nothing preventing the compiler from storing some meta-data if it's needed later.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.