f



Compile time of C++ vs C#

Hi,

I am curious to know why C# code much faster than a similar sized C++
code. How does MSIL help? Does having a common base class like object
help in reducing compile times?

Thanks,
Shireesh
[The optimizer is usually the slowest part of a compiler and I would guess that
MSIL offers fewer opportunities than native code. -John]

0
shirsoft (12)
9/1/2009 8:15:35 PM
comp.compilers 3310 articles. 1 followers. Post Follow

6 Replies
975 Views

Similar Articles

[PageSpeed] 24

On 2009-09-01, Shirsoft <shirsoft@gmail.com> wrote:
> I am curious to know why C# code much faster than a similar sized C++
> code. How does MSIL help? Does having a common base class like object
> help in reducing compile times?

Which C++ compiler? Does it support precompiled headers?

> [The optimizer is usually the slowest part of a compiler and I would guess that
> MSIL offers fewer opportunities than native code. -John]

I'd bet on not parsing include files and not restarting the compiler binary
for every compilation unit.

0
Marco
9/2/2009 9:49:04 AM
Shirsoft schrieb:

> I am curious to know why C# code much faster than a similar sized C++
> code. How does MSIL help? Does having a common base class like object
> help in reducing compile times?

The less ambiguous syntax, simplified preprocessor and true precompiled
header files (assemblies) speed up C# compilation.

> [The optimizer is usually the slowest part of a compiler and I would guess that
> MSIL offers fewer opportunities than native code. -John]

ILASM also doesn't have to create code for an horrible instruction set
(e.g. x86), has no register allocation pressure or instruction ordering
requirements. All this is done by the JIT compiler.

DoDi

0
Hans
9/2/2009 1:23:17 PM
"Shirsoft" <shirsoft@gmail.com> wrote in message
> I am curious to know why C# code much faster than a similar sized C++
> code. How does MSIL help? Does having a common base class like object
> help in reducing compile times?

invoking my experience as a compiler writer, I can give a fairly solid
guess:
the parser.

basically, in C and C++, a non-trivial amount of time and effort goes into
getting the code parsed (in my case, parser-related tasks are the majority
of the total time).

the main reason for this: headers.


now, in a language like C#, there are no headers or inclusion.
instead, it is possible to pool everything, and essentially process the
entire assembly at once, and the parser manages to work, initially, with an
incomplete understanding of the language (apparently, I am not entirely
certain how it works though...).

my guess is that it potentially uses 2 passes to do parsing:
a first pass to basically figure out all of the declarations (the top-level
structure for the assembly, skipping over function contents, ...);
a second pass to basically parse all of the code (at this point, it knowing
the top-level structure, so it can employ an exact parser).

the first pass may not actually do a full parser (just guessing here), but
may rather use a heuristic/approximate parser, where it looks for items of
interest (via pattern matching) but ignores everything else.

I partially guess this by noting that C# imposes a few restrictions on the
syntax similar to those I employ for my automated header-generation tools,
which similarly use a pattern-matching parser strategy (it does not parse
the full syntax, rather it looks for things which look like declarations,
and I constrain the declarations such that the tool finds them).

noting that MS's stuff uses pattern-matching at the machine code level,
little is to say they don't do similarly for processing C# syntax as well.


hence, much faster parsing.

at this point, it can do whatever, and spew out the bytecode.
I don't suspect all that much work is done at this point (judging from my
compiler, it is very possibly just a few sorts of micro-optimizations, and
more or less flattening the AST's into bytecode).


> Thanks,
> Shireesh
> [The optimizer is usually the slowest part of a compiler and I would guess
> that
> MSIL offers fewer opportunities than native code. -John]
>

judging from MSVC's ASM output on x64, well, their optimizer is not exactly
the best around...

basically, I infer that it doesn't even figure out some really basic/obvious
things, so I have some doubts that their optimizer is exactly what is using
up a lot of the time.

what gives me this impression:
movss [rsp+20h], xmm0
movss xmm0, [rsp+20h]

be ready to see things like this, a lot...

I suspect their optimizer is unable to figure out things like, for example,
that the value of a given variable is still located in a register, ...


similarly, they seem to copy structs around via this particular gold nugget:
"
mov rsi, ...
mov rdi, ...
mov rcx, ...
rep movsb
"...

as opposed, to, say, moving the data around using registers.
granted, I have done little to test or prove that register-based moving is
faster, but I would think it would be.

for example, for a 16 byte struct:
movups xmm0, [...]
movups [...], xmm0

(or 'movaps', if it can be determined that the memory is aligned).


actually, apparently many of their optimizations are not so much about
generating highly optimized code, but rather in turning off things that are
rather questionable and serve to do little more than eat clock cycles
("well, who knows, maybe a user will do a division by zero and expect to get
a nice little status code from errno...").


my guess is that, for whatever reason, MS is just not "that" concerned about
performance on x64.

(and, I have noticed that my code manages to run maybe 2x-3x slower than x86
and gcc, from what I can tell...).

so, yeah, I am left doubting that this is where most of the compile-time is
going.

0
BGB
9/2/2009 8:16:10 PM
On Tue, 1 Sep 2009 13:15:35 -0700 (PDT), Shirsoft <shirsoft@gmail.com>
wrote:

>Hi,
>
>I am curious to know why C# code much faster than a similar sized C++
>code. How does MSIL help? Does having a common base class like object
>help in reducing compile times?
>
>Thanks,
>Shireesh
>[The optimizer is usually the slowest part of a compiler and I would guess that
>MSIL offers fewer opportunities than native code. -John]

Maybe, but I would have thought templates have a lot to answer for WRT
compile times in C++. C# has generics, but they probably use a
relatively simple model - the generic is compiled separately, but a
closure-like structure is used to fill in instance parameters. C++
templates are more like a kind of broken Haskell that gets interpreted
at compile-time to generate new code.

Obviously Haskell can be compiled, and templates could in principle be
compiled in much the way that many (most?) scripting languages are
compiled to a virtual machine model these days even when used
interactively. I dare say recent compilers do a lot to optimise
template handling. Still, I expect there's a fair amount of overhead
there just from using the standard library. And if you have a template
library that's prone to bloat, of course, you have a slow build
because of all the template instantiations that are being generated
and optimised.

0
Stephen
9/3/2009 3:44:29 AM
"Marco van de Voort" <marcov@stack.nl> wrote in message
> On 2009-09-01, Shirsoft <shirsoft@gmail.com> wrote:

<snip>

>> [The optimizer is usually the slowest part of a compiler and I would
>> guess that
>> MSIL offers fewer opportunities than native code. -John]
>
> I'd bet on not parsing include files and not restarting the compiler
> binary for every compilation unit.

agreed...

this is related to a recent idea I had for speeding up C compile times in my
framework (not yet implemented though):
I can compile groups of files at a time, essentially lumping them together
so that all share more or less the same state (AKA: trying to include the
same header again will almost invariably prune away its contents...).

this way, the several MB of random crap typically pulled in from headers
only happens once for all of the files in the particular grouping (leaving
instead a combined lump of maybe the several hundred kB of all of the stuff
in the grouping).

this "should" allow compiling a group of files in similar time to compiling
a single file.

however, either way, I still need to wait the many seconds or so for all
this to compile, so this would be more a means of improving the performance
scalability, rather than reducing the total cost of compilation.


the other option, of which I have been perpetually too lazy to mess with, is
to clean up and reorganize my project headers such that compiling things
tends to pull in far less huge amounts of random stuff.


there is a possible cost though:
compiling files in a group is not strictly semantically equivalent to
compiling them individually, which "could" raise issues in some rare cases
(or bring up standards-conformance concerns).

for example, stuff which, technically, shouldn't be visible in a given
compilation unit, may be visible.
another possible risk is that of compilation units having conflicting stuff,
but I think this should be rare in practice, and is unlikely to pop up in
situations in which grouping would be used (most likely for collections of
files representing a common library or subsystem).


from observations, I suspect MSVC may be doing something vaguely similar
when one passes a whole bunch of files on the commandline to produce a DLL
(my noticing how MSVC manages to go through the files much quicker than GCC
does, typically several per second vs one every few seconds, and as well how
it seems to give all of its warning/error messages all at once apparently
mishmashed between all the various stuff passed in on the command line).

however, I don't know the details, such as, for example, whether MSVC has a
mechanism to avoid cross-visibility between compilation units, or even what
exactly they are doing, ...


so, ammusingly, it seems in rebuilding my project, much more of the time is
apparently going into 'make' than is into 'cl'.

'make', however, does not seem to be a significant user of processor time
(and as well, CPU load is very low when make is doing its thing), making me
suspect it is mostly IO-bound...


so, yeah, MSVC has its good points and its bad points...

0
BGB
9/3/2009 4:55:30 PM
You may be comparing apples to oranges. Managed languages like C# and
Java running on a VM offer two kinds of compilers: a
source-to-bytecode compiler that is called by the user and one or more
bytecode-to-native code compilers (often called JIT compilers) that
are under the control of the VM. The former (by design) perform next
to no optimizations while the latter may do so. The C++ compiler is
more akin to an optimizing JIT compiler than it is to the bytecode
compiler.

-- O.L.

0
Olivier
9/3/2009 11:41:53 PM
Reply:

Similar Artilces:

program that compiles in C compiler but not in C++ compiler
Hi, I need a small program that compiles in C compiler but not in C++ compiler. Thx in advans, Karthik Balaguru KBG <karthik.balaguru@lntinfotech.com> wrote: > I need a small program that compiles in C compiler but not in C++ > compiler. No problem, just send $10 to paypal@zevv.nl and I'll do your homework for you. -- :wq ^X^Cy^K^X^C^C^C^C KBG said: > Hi, > > I need a small program that compiles in C compiler but not in C++ > compiler. Can you think of any syntactic differences between C and C++? For example, what about keywords? They are very, very sen...

Are sun studio C/C++ compilers and Forte C/C++ compilers same ??
Is there any difference between sun studio compilers and forte compilers??? or the names have been changed??? In article <1145338052.955429.256610@v46g2000cwv.googlegroups.com>, "ameya_agnihotri" <ameyaagnihotri22@gmail.com> writes: > Is there any difference between sun studio compilers and forte > compilers??? > or the names have been changed??? One of many name changes in the product's history (including amongst others, Java somethingortheother, Workshop, Proworks/Teamworks, and probably more I've forgotten). Of course, the versions and features cha...

commercial c compilers vs free c compilers
what are the benefits in using commercial compilers like intel, greenhill or portland compared to free c compiles like gcc, lcc, tcc, TenDRA, sun and others? geletine wrote: > what are the benefits in using commercial compilers like intel, > greenhill or portland compared to free c compiles like gcc, lcc, tcc, > TenDRA, sun and others? > <mostly non-topical> As we have no idea of your interests, potential benefits may not be meaningful in your projects. More than one of these commercial compilers is more efficient about vectorization, although gcc is closing the gap. <...

C vs. C++ and C compiler optimization question
/* Consider the following C snippet: */ Foo *pFoo = NULL; void FooLength(Foo *pFoo); void FooHello(Foo **ppFoo); void FooWorld(Foo **ppFoo); void FooLength(Foo *pFoo) { int count = 0; for (Foo *p = pFoo; *p; p++) ++count; return count; } void FooHello(Foo **ppFoo) { int count = FooLength(*ppFoo); // ... } void FooWorld(Foo **ppFoo) { int count = FooLength(*ppFoo); // ... } // Consider the following C++ snippet: class FooHelloWorld { public: void FooHello(Foo **ppFoo) { iCount = FooLength(*ppFoo); // ... } void FooWorld(Foo *...

Motorola PowerPC C Compiler 1.5 vs GNU C Compiler
Hi, I have a copy of the PowerPC C Compiler 1.5 from Motorola for an embedded board with a PPC603e processor. I want to replace the compiler with the GNU toolset chain and eventually port Linux over to this board. Is the MOT Compiler actually GCC? Is the MOT compiler and GCC binary compatible?? Is the MOT compiler freely distributable??? Thanks, Ram rmeenaks@olf.com wrote: > Hi, > > I have a copy of the PowerPC C Compiler 1.5 from Motorola for an > embedded board with a PPC603e processor. I want to replace the > compiler with the GNU toolset chain and eventually por...

Compiled C vs C++
Hi, since I've done a bit of Java recently, I'm thinking of using some C++ instead of C, just because the "class" syntax is looking more convenient now. One thing that makes me wonder is C++ code size though. I've heard that C++ is supposed to be just a souped up C that makes you "pay as you go", and I don't intend to go anywhere far. But a small toy class I wrote generated much more code than the equivalent C struct, when instantiated in a test main(). The C code did just what it was supposed to: malloc, fill some struct fields and return. The C...

Will C pgm compile on C++ compiler?
I had C programming in school years ago and would like to play with it again. When I go online I only seem to be able to find C++ compilers. I know that C++ is object oriented, whereas C is not. I can get a good price on Visual C++ 6.0. Is there a way to run native C programs on a C++ compiler? Thanks, Ken [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] "Ken Applequist" <apple07840@yahoo.com> wrote in message news:<Sh%1d.4320$F75.1229@trndny01>... > I had C programming in school ...

Could I compile "c" source with a C++ compiler (Forte C++ Update 2)?
Does Forte C++ Update 2 compiles C source code in ANSI C as same as Forte C udpate 2 does? Jacob, Park <bluejacob@empal.com> wrote: > Does Forte C++ Update 2 compiles C source code in ANSI C as same as > Forte C udpate 2 does? If you're asking whether "CC" will compile the same code as "cc", then the answer is no. This is true for ANY standard-compliant c++ compiler (compilers with different "language mode" like gcc don't count as a single compiler for this description). However, usually one can make C code "compilable" by C++ co...

compiling c code but libraries are compiled in c++
hi, We have written cunit code for testing some APIs. defination of those APIs are in c++ and .so library is created of them. when I compile my cunit code it gets compiled successfully but while liking it gives undefined reference to symbol. can anyone help me in this matter. Regards Sumit Shrivastava On 29 May 2007 04:11:14 -0700 sumit <sumit.shrivastava09@gmail.com> wrote: | We have written cunit code for testing some APIs. defination of | those APIs are in c++ and .so library is created of them. when I | compile my cunit code it gets compiled successf...

Writing a C/C++ compiler in C++
I've been thinking of writing a C++ compiler in C++ over the next several years, and I was wondering what's changed in writing a compiler? I've been pouring over these groups and there seems to be a lot of tools for starting but is the modern process still the same? As far as I understand it you write a lexer that tokenizes all the symbols, then write a parser that parses all of the tokens, then walks the parse tree to generate the asm or IL that gets shoved to the backend. So it seemed straightforward except that C++ isn't a LALR(1) grammer that Bison or Byacc accepts. Does ...

Re: Will C pgm compile on C++ compiler?
Ken Applequist wrote: > I had C programming in school years ago and would like to play with it > again. When I go online I only seem to be able to find C++ compilers. I know > that C++ is object oriented, whereas C is not. I can get a good price on > Visual C++ 6.0. Is there a way to run native C programs on a C++ compiler? Several authors implied that a C++ compiler can compile C programs. This is only correct with some restrictions. For example, you can obviously not use C++ keywords as identifiers. For example, the following is a legal C program but will fail using C++: in...

link a c compiled application with a c++ compiled library ?
Hi, I've searched nearly one hour for this but found nothing usefull :-/ So my question is simple : Is it possible to link a c compiled application with a C++ compiled library ?? If yes, how would it possible.. (I guess this would be indirectly posible by using some wrapper around the c++ library, but I'm aboslutely not wanting to do that in my case, well at least if I have to write that wrapper manually myself). I looked at gcc,g++ man pages, gcc online documentation, .. but can't even find a beginning of info related to that :-/ Thank you if you can help me. Regards, ...

Compiling C into C++
I'm trying to include a C header file from an external library in my C+ + program but when I compile I get the following error: error: expected unqualified-id before 'private' error: abstract declarator 'void*' used as declaration error: expected ';' before 'private' It didn't take me long to figure out that the offending C header file uses a variable with the name "private". I've reproduced the problem as follows: /* -------- a.h - the C header file */ #ifndef TEST_H #define TEST_H struct s { void *private; }; #endif /* -------- end...

which c/c++ compiler?
Hi all, I'd like to know if Microsoft Visual Studio .NET 2003 can be used for real-time applications. I'm a new user of Matlab and I'm trying to use Simulink and Real-Time Windows Target for acquiring data coming from an accelerometer. I've read in RTWT tutorial that is recommended the use of Microsoft Visual C/C++ or Watcom compiler..... Bye! Hi, Read the solution give at the following link, http://www.mathworks.com/support/solutions/data/1-1BRI9.html?solution=1-1BRI9 Bye! On Sun, 05 Sep 2004 12:32:21 GMT, Alessandra wrote: >Hi all, >I'd like to know if Microso...

Web resources about - Compile time of C++ vs C# - comp.compilers

Compile (publisher) - Wikipedia, the free encyclopedia
Compile was a Japanese videogame developer, most notable for having developed the Puyo Puyo series, based on their Madou Monogatari franchise, ...

Information Is Power: Facebook Develops ThreatData To Compile Data On Web Threats
Part of being able to combat malware, phishing, and other online threats is gathering and consolidating as much data on those threats as possible, ...

Credit Suisse compiles yuan winners and losers list: report
... the lowers. A list of Australian winners and losers from the decision by China’s government to devalue the renminbi has reportedly been compiled ...

Beijing residents compile own death toll in flooding
Beijing residents fed up with a lack of official updates are compiling their own death tolls for last weekend's deadly floods in the capital, ...

New report compiles 25 years of UFO sightings in Canada
A Winnipeg group called Ufology Research has compiled and analyzed reported sightings of UFOs across Canada over the last 25 years.

China's nat'l library to compile book on Diaoyu Islands
China's nat'l library to compile book on Diaoyu Islands People's Daily Online ... States, Australia and the United Kingdom. The book is aimed ...

Googlers compile holiday search tips in rap video to help Santa with flight
Google is in full holiday swing (what, you haven't heard?). They just posted, on YouTube, a rap video that was put together by its employees, ...

Popular Dating Site Compiles 15 Stupid Reasons To Date A Lawyer
A new listicle describes a really horrible person to date and then says, “we think lawyers are probably this awful, go date them!” Continue ...

Author Compiles Lively L.A. Times History Lesson
Long before Eli Broad , Rupert Murdoch and people willing to pay $140 million for the Las Vegas Review-Journal, there was Harrison Gray Otis ...

Japan, China compile written pledge to improve ties ahead of summit
Japan and China reached a rare written agreement on Nov. 7 to improve relations strained by a territorial dispute over the Senkaku Islands in ...

Resources last updated: 1/24/2016 9:33:32 AM