Click here to Skip to main content
15,885,767 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
is there a differents between these codes on speed on how the compiler create the .exe ?

C#
String s = Object1.Object2.GetCustomerName();// the Name can also be a Property
CallMethod(s);


and

C#
CallMethod(Object1.Object2.GetcustomerName());


The second would just have less code if this is done in the whole project. If there would be bigger things like this it would just be hard to read the code.

The first would be very helpfull at debugging because you can see very fast the value you call the method. And the code would be much cleaner.

Am i saying something wrong?

Thanks,
Posted
Comments
Gabriel Sas 22-Nov-13 14:25pm    
in generally looking on the release version. because i know that in release the compiler does some optimizations, but i don't know exactly how it does it

In practice? No, not in release versions - the intermediate variable will be optimised out.
In debug versions there is likely to be a difference as debug versions generally do not employ as aggressive optimisations, if it uses any at all.

You can check for yourself: this explains how to view the IL http://www.ginktage.com/2011/04/display-il-code-in-visual-studio-2010/[^]


But...having said that most of the optimisation is done at the JIT compiler which uses the IL as the input, and doesn't really care if you are debugging or not... http://blogs.msdn.com/b/ericlippert/archive/2009/06/11/what-does-the-optimize-switch-do.aspx[^]

It's complicated: Personally I would use the first version because it is easier to read, and to debug - but in practice it will make no difference at all! :laugh:
 
Share this answer
 
Compile it and see!

You can easily compare compiled exe's using ildasm (Intermediate Language Disassembler) that is included with Visual Studio.

Also another fun trick is to compare two of exactly the same programs, one compiled for Debug, and the other compiled for Release.

As far as your question goes, if you compile the two under Release mode, the resulting code should be the same. The compiler will optimize away the "s" variable and basically make exactly the same output in both.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 22-Nov-13 17:20pm    
Best answer so far, a 5, but perhaps you did not pay attention for something else — please see my answer.
—SA
In addition to other answers: I would advice you to focus on code maintenance and readability and ignore the possibilities for such small "optimizations": most usually, they won't actually "optimize" anything. As to the readability differences between your two samples, there is no clear preferences. It depends. There is nothing wrong with second option, it's generally better. First is questionable, but it depends on the context.

I would say, the best way to optimize code is not doing anything stupid. But let's look at the semantics of your code fragment. And I hate to send: the idea calling a method by name is certainly, decisively, impossibly, impossibly silly. I don't even discuss this idea, unless you explain your goal and want to discuss it. Better forget about it and never do such things.

—SA
 
Share this answer
 
Comments
Gabriel Sas 22-Nov-13 17:34pm    
i have asked this question and added a little code to explain the idea behind.

the question came in when a colleague told me that he is trying to write as little code as possible to do something and i immediately thought about how hard can be to read, maintain and debug such code and also if there would be a performance impact on the application if the code would be easy to read, maintain and debug. he didn't had an answer when i asked him why? and just replied something like "the old guys(over 40 years old) are doing this and i'm learning from them"(this guy is over 30)

anything else needed to be said?
Sergey Alexandrovich Kryukov 22-Nov-13 18:21pm    
I don't understand how it's related to anything. Anyway, calling anything by a name... someone is lost really well... I'm not sure if someone spent 30 or 40 years in a worthy way. :-)
—SA
Matt T Heffron 22-Nov-13 20:25pm    
I've been programming for over 40 years and, like Sergey said, this is extremely silly!
It seems your colleague needs to find better role models/mentors.
Sergey Alexandrovich Kryukov 22-Nov-13 20:28pm    
:-)
Gabriel Sas 25-Nov-13 12:56pm    
could you give an example on that "calling method by name is bad/silly", i get it in some ways, but i actually don't get what you are really trying to explain.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900