How sophisticated is VS compiler?

This is a discussion on How sophisticated is VS compiler? within the C# Programming forums, part of the General Programming Boards category; I am wondering, will VS compiler optimize this: Code: myObj.weap.mymy.ariari += "optimized?"; uselessBox.Show(); myObj.weap.mymy.ariari += "eh, eh?"; .... myObj.weap.mymy.ariari += ...

  1. #1
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853

    How sophisticated is VS compiler?

    I am wondering, will VS compiler optimize this:
    Code:
    myObj.weap.mymy.ariari += "optimized?";
    uselessBox.Show();
    myObj.weap.mymy.ariari += "eh, eh?";
    ....
    myObj.weap.mymy.ariari += "still?";
    myObj.weap.mymy.ariari += "howaboutnow??";
    I want it to actually do this:
    Code:
    string tmpStr = myObj.weap.mymy.ariari;
    uselessBox.Show();
    tmpStr += "eh, eh?";
    ....
    tmpStr += "still?";
    tmpStr  += "howaboutnow??";
    So it doesn't search everytime to find ariari through 3 references. The answer would be "of course" if there wasn't for "uselessBox.Show();" for all the compiler now can alter ariari so tmpStr will point to nowhere. Of course it could try to check that, so my question still stands

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    Just believe in the compiler, would be my answer. The compiler will apply optimizations that it knows will work. But then again, seeing as this is C#, the compiler will probably do nothing and the CLR will do the optimization on-the-fly instead.

    Besides that, I fail to see how the code is related.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I HATE that there are references and value types in C#. I like simple things that you know exactly what you do. Sigh, anyway.

    Why is my code non-related by the way? What I want is this (in C++):
    Code:
    string* tmpStr= myObj.weap.mymy.ariari;
    ...
    where ariari is string* of course. Well, in C# you have references. So tmpStr would refer to the same objec ariari refers to, right? So I can use tmpStr instead of ariari to refer to the string object... It won't just have to check where myObj referes to and then where weap referes ... where ariari refers to

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I get that, but how does uselessBox fit into the picture?
    Besides, it doesn't matter if you use tmpStr or the fully qualified path; it does the same thing, so why should the compiler care?

    Oh and I also hate C# because of the things it hides.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    If it doesn't then it's very stupid.

    At the highest optimization level, it should do as much as it can at compile-time and run-time.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Well, in my mind a reference is a pointer. So I translate it:
    Code:
    myObj->weap->mymy->ariari
    which is slower than using ariari directly. To use it directly though you need a reference to it, that is what tmpStr is.

    Anyway, assuming that is actually slower, then the compiler would optimize it since it used a lot of times. It will store somewhere the value of ariari and use that instead of searching everytime. My point about calling uselessBox.Show() a function in C(#) has side effects. So Show() could have (even though it hasn't):
    Code:
    ...
    ariari = new Object();
    ...
    then there would be a difference between tmpStr and myObj.weap.mymy.ariari. That is the reason the path has to be calculated everytime, because it might change from a function. If there are no functions then there are no side effects and such optimizations are easy to implement

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,422
    I think it is quite common for a compiler to store the address of something to be changed into a register and if it detects the exact same thing being changed later, it simply uses that cached address.
    But again, this is really the work of the CLR, not the compiler.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Your two samples give different results. Strings are immutable so in your second sample myObj.weap.mymy.ariari won't be changed at all.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  9. #9
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    immutable... yeah, that was the thing I was missing. Thanks. Do you know, by the way, why are they immutable?

  10. #10
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Harder to make a mistake. Like if two classes point to the same (mutable) string and one changes it, it will also change for the other class which probably is undesired. Making strings immutable solves this.

    If you are looking for a mutable string however, check out System.Text.StringBuilder.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 05:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 10:28 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21