473,580 Members | 2,882 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

optimized code

Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?

Are simple common sub-expressions and loop invariants optimized out in the
current optimizer?

thanks,
m
Dec 6 '05 #1
10 2133

"Mike" <vi********@yah oo.com> wrote in message
news:Oj******** ********@TK2MSF TNGP10.phx.gbl. ..
Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?


Yes, the Managed C++ compiler should still be better. the C# compiler still
doesn't go very far as far as optimizations go, AFAIK anyway. They are left
to the JIT.
Dec 6 '05 #2

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.


Thanks. Well, I guess I don't care if the compiler does the magic, or the
JIT.
I assume the JIT is limited to peephole optimizations? Does the JIT handle
common sub-expression removal, loop invarients, etc?

If I have an inner loop of something like:
for ( i ...) {
a.b.c.x = ...
a.b.c.y = ...
a.b.c.arr[i] =
...
}

Do I need to put a.b.c in a local outside the loop, or will the JIT take
care of this?
Sometimes it's cleaner to code this way, sometimes not - but there's also
auto-generated code, and whether I need to generate code with temps.

thanks

Dec 6 '05 #3
Interesting, re: managed C++:
http://msdn.microsoft.com/msdnmag/is...Optimizations/
"Mike" <vi********@yah oo.com> wrote in message
news:ui******** ********@TK2MSF TNGP10.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.


Thanks. Well, I guess I don't care if the compiler does the magic, or the
JIT.
I assume the JIT is limited to peephole optimizations? Does the JIT
handle common sub-expression removal, loop invarients, etc?

If I have an inner loop of something like:
for ( i ...) {
a.b.c.x = ...
a.b.c.y = ...
a.b.c.arr[i] =
...
}

Do I need to put a.b.c in a local outside the loop, or will the JIT take
care of this?
Sometimes it's cleaner to code this way, sometimes not - but there's also
auto-generated code, and whether I need to generate code with temps.

thanks

Dec 6 '05 #4

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

"Mike" <vi********@yah oo.com> wrote in message
news:Oj******** ********@TK2MSF TNGP10.phx.gbl. ..
Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?


Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.

The same applies to the "managed C++" compiler, common sub-expressions and
loop invariants are handled by the JIT not by the compiler fron-ends.
The managed C++ compiler generates IL, sometimes it does a better job, but
I've seen IL code that was better optimized by the C# compiler. Anyway the
differences are < 5% (both sides), MS said that they will focus on the JIT
to further optimize, not in the C++ front-end.
Willy.
Dec 6 '05 #5

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uh******** ********@TK2MSF TNGP12.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

"Mike" <vi********@yah oo.com> wrote in message
news:Oj******** ********@TK2MSF TNGP10.phx.gbl. ..
Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?


Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.

The same applies to the "managed C++" compiler, common sub-expressions and
loop invariants are handled by the JIT not by the compiler fron-ends.
The managed C++ compiler generates IL, sometimes it does a better job, but
I've seen IL code that was better optimized by the C# compiler. Anyway the
differences are < 5% (both sides), MS said that they will focus on the JIT
to further optimize, not in the C++ front-end.


Ya, for the most part IL doesn't lend itself to much optimization. You can't
stash locals in registers or play memory tricks to boost performance. Still,
the C++ code should be dumped through the optimization layer in the C++
compiler before its emitted. I'd guess they go faruther, even if the results
aren't always obvious.

I am a little surprised the C++ compiler doesn't do some of hte more basic
optimizations that it should be entirely capable of doing.
Dec 7 '05 #6

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uh******** ********@TK2MSF TNGP12.phx.gbl. ..
The same applies to the "managed C++" compiler, common sub-expressions and
loop invariants are handled by the JIT not by the compiler fron-ends.
The managed C++ compiler generates IL, sometimes it does a better job, but
I've seen IL code that was better optimized by the C# compiler. Anyway the
differences are < 5% (both sides), MS said that they will focus on the JIT
to further optimize, not in the C++ front-end.


This does not seem to jibe with:
http://msdn.microsoft.com/msdnmag/is...Optimizations/

---snipped from above page---
Another common misconception is that the same kind of superior performance
on the .NET Framework can be attained regardless of the language you
use-that the generated Microsoft intermediate language (MSIL) from various
compilers is inherently equal. Even in Visual Studio .NET 2003 this was not
true, but in Visual Studio 2005, the C++ compiler team went to great lengths
to make sure that all of the expertise gained from years of optimizing
native code was applied to managed code optimization. C++ gives you the
flexibility to do fine tuning such as high-performance marshaling that is
not possible with other languages. Moreover, the Visual C++ compiler
generates the best optimized MSIL of any of the .NET languages. The result
is that the best optimized code in .NET comes from the Visual C++ compiler.

....

With Visual C++ 2005, the compiler can perform a large subset of the
standard native code optimizations on MSIL code. These include everything
from dataflow-based optimizations to expression optimization to loop
unrolling. This level of optimization is unmatched in any other language on
the platform. In Visual C++ .NET 2003, Whole Program Optimization (WPO) was
not supported for builds using the /clr switch, but Visual C++ 2005 adds
this capability for managed code. This feature enables cross-module
optimization, which I'll discuss later in the article.

The only class of optimizations not available for managed code in Visual C++
2005 are Profile Guided Optimizations, although they may be available in a
future version. For more information, see Write Faster Code with the Modern
Language Features of Visual C++ 2005

....

The optimizations done on MSIL code are a large subset of those done on
unmanaged code. It's worth pointing out that the class of allowable
optimizations is different depending on whether the compiler is generating
verifiable code (/clr:safe) or unverifiable code (/clr or /clr:pure). A few
examples of the types of things that the compiler can not do because of
either metadata or verifiability constraints include strength reduction (to
convert multiplications into pointer addition), and inlining the access of
private members of one class into the method body of another class.

After the MSIL code is generated by the Visual C++ compiler, it is then
consumed by the JIT. The JIT reads in the MSIL and begins to perform
optimizations that are very sensitive to changes in the MSIL. One
instruction sequence of MSIL might be very amenable to optimization, whereas
another (semantically equivalent) sequence stifles optimization. For
example, register allocation is an optimization in which the JIT optimizer
attempts to map variables to registers; registers are what the actual
hardware uses as operands to perform arithmetic and logical operations. At
times, semantically equivalent code, written in two different ways, might
cause the optimizer to have a more difficult time performing good register
allocation. Loop unrolling is an example of a transformation that might
cause the JIT to have problems register-allocating.

Loop unrolling done by the C++ compiler can expose more instruction-level
parallelism, but can also create more live variables that the optimizer
needs to track for register allocation. The CLR JIT can only track a fixed
number of variables for register allocation; once it has to track more than
this, it begins to spill the contents of registers into memory.

For this reason, the Visual C++ compiler and the JIT must be tuned in tandem
to generate the best code. The Visual C++ compiler is responsible for those
optimizations that are too time-consuming for the JIT and those for which
too much information would be lost in the compilation process from C++
source to MSIL.


Dec 7 '05 #7
Daniel O'Connell [C# MVP] <onyxkirx@--NOSPAM--comcast.net> wrote:

<snip>
I am a little surprised the C++ compiler doesn't do some of hte more basic
optimizations that it should be entirely capable of doing.


While I can't say for sure that the same would be true in .NET, in Java
Sun found that the more they tried to optimise the bytecode, the harder
it was to optimise at JIT time. Optimisation can make the code more
complicated by trying to guess what will happen to it, whereas a JIT
can optimise well when it can easily understand the meaning of the
code.

For instance, if a compiler does loop unrolling in the IL, the JIT
would have a difficult job doing a different level of loop unrolling in
the JITted code, even though it knows better how much processor cache
is available (which may not currently be a factor in how much loop
unrolling the JIT does, but it easily *could* be).

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 7 '05 #8
Can anyone comment on C# vs. C++ for floating point performance with V2?

In
http://msdn.microsoft.com/library/de...etperftips.asp
we
read:

"Floating Point—The v1 JIT does not currently perform all the FP-specific
optimizations that the VC++ backend does, making floating point operations
more expensive for now. "

Has this been improved? Can anyone quantify the difference in performance.
I have a highly computation-intensive program written in C# and wonder what
the difference would be if I were to rewrite in C++.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uh******** ********@TK2MSF TNGP12.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

"Mike" <vi********@yah oo.com> wrote in message
news:Oj******** ********@TK2MSF TNGP10.phx.gbl. ..
Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?


Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.

The same applies to the "managed C++" compiler, common sub-expressions and
loop invariants are handled by the JIT not by the compiler fron-ends.
The managed C++ compiler generates IL, sometimes it does a better job, but
I've seen IL code that was better optimized by the C# compiler. Anyway the
differences are < 5% (both sides), MS said that they will focus on the JIT
to further optimize, not in the C++ front-end.
Willy.

Dec 7 '05 #9
All depends on what exactly you are measuring , how you are comparing and
what you are expecting.
But suppose we are talking about code generated by MSFT VS2005 C#, C++ and
C++/CLI, first, you should understand that you are basically using the same
math library (the CRT library) for all three. That means that the results
should be comparable for both C# and C++/CLI, and that C++ should take
advantage of the back-end optimizer which can do a better job than the JIT
compiler.
Anyway what I have measured so far using VS2005, is a slight advantage (<
5%) for C++/CLI over C# and a larger advantage (<20%) for C++ oner C#.

The difference between C# and C++/CLI is due to a better optimized IL
produced by the C++ compiler, when enabling max. optimizations (enables the
fastest FP unit mode).
The difference between managed and unmanaged C++ is due to the extra
redirection (through the CLR) between managed code(well, JIT'd ) and the
math library.
Note that your mileage may vary, therefore, I would suggest you do your
homework, run some benchmarks, compare and make a language decision. Note
however, that performance should never be the only criterion to select a
development language (or platform). Remember faster is not always better,
especially when talking about FP arithmetics.
Willy.
"Fred Mellender" <no************ ****@frontierne t.net> wrote in message
news:YT******** *******@news02. roc.ny...
Can anyone comment on C# vs. C++ for floating point performance with V2?

In
http://msdn.microsoft.com/library/de...etperftips.asp
we
read:

"Floating Point—The v1 JIT does not currently perform all the FP-specific
optimizations that the VC++ backend does, making floating point operations
more expensive for now. "

Has this been improved? Can anyone quantify the difference in
performance. I have a highly computation-intensive program written in C#
and wonder what the difference would be if I were to rewrite in C++.

"Willy Denoyette [MVP]" <wi************ *@telenet.be> wrote in message
news:uh******** ********@TK2MSF TNGP12.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:eN******** ********@TK2MSF TNGP09.phx.gbl. ..

"Mike" <vi********@yah oo.com> wrote in message
news:Oj******** ********@TK2MSF TNGP10.phx.gbl. ..
Is it still true that the managed C++ compiler will produce much better
opimizations than the C# compiler, or have some of the more
global/aggressive opimizations been rolled into the 2005 compiler?
Yes, the Managed C++ compiler should still be better. the C# compiler
still doesn't go very far as far as optimizations go, AFAIK anyway. They
are left to the JIT.

The same applies to the "managed C++" compiler, common sub-expressions
and loop invariants are handled by the JIT not by the compiler fron-ends.
The managed C++ compiler generates IL, sometimes it does a better job,
but I've seen IL code that was better optimized by the C# compiler.
Anyway the differences are < 5% (both sides), MS said that they will
focus on the JIT to further optimize, not in the C++ front-end.
Willy.


Dec 7 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

133
8489
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
6
3697
by: SSG | last post by:
Hai All! I need the optimized code for finding the string length. i dont want to use strlen function......... can anyone know reply........ By S.S.G
8
4600
by: Fuzzy | last post by:
I want to experiment with the JIT compiler's optimization of code, especially regarding inline compilation of small methods. What I have done is set the Build options for 'Optimize Code' to TRUE and set 'Generate debugging information' to TRUE. I run the compiled application from Windows Explorer, crank up Visual Studio .NET, and attach to...
8
1329
by: Jarod | last post by:
Hey Is VS2005 optimized for Intel or AMD processor ? What gives best speed when I develop webprojects ? What really means in the term of speed 2 cores or frequency I assume 64bits. Jarod
6
1750
by: Bran Kelly | last post by:
Hi, I am running into something I haven't ever encountered before. I have a static instance of a class declared in a cpp file, which seems to be optimized (or for some other reason) out of existence. It is something like the following: foobar.hpp class foobar {
17
2092
by: Grizlyk | last post by:
Hello. What can be optimised in C++ code and how i can garantee stable behaviour below 1. Are expression "auto volatile" can deny removing as "unused temporary" like this: auto volatile const class_name tmp;
7
5616
by: bonk | last post by:
I have a c# project as part of a larger VS 2005 solution that always gets build optimized and I therefore can not evaluate any values while debugging through the code ("Cannot evaluate expression because the code of the current method is optimized."). This happens alltough the checkbock "optimize code" in the project settings is switched off....
4
23205
by: Glenn | last post by:
OK, I've looked up this message but am not finding how to get rid of it: "Cannot evaluate expression because a thread is stopped at a point where garbage collection is impossible, possibly because the code is optimized" I'm trying to debug, and it's rather difficult when I can't find the values of expressions because of whatever is causing...
7
2559
by: Zytan | last post by:
I have the simplified build ("show advanced build configurations" turned off), so that pressing F5 runs in DEBUG mode with the debugger. When an assertion fires, I find that I cannot 'watch' some data, it explains that "Cannot evaluate expression because the code of the current method is optimized". I know that I can go into Project- My...
0
7781
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8130
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8295
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6530
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5664
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5347
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3788
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1392
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1117
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.