execution speed c++ versus c#

D

Dan here !

I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which is close
to optimal
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.

Thank you
Dan
 
J

Jerry Coffin

[ ... ]
I have always heard that C++ compiles into machine code which is
close to optimal however C# is converted into p-code. So the speed
question could be, how does p-code compare to machine code.

That's not really the right question. A typical VM includes a "just
in time" compiler. This will dynamically convert (at least parts of)
the P-code to machine code for execution.

That means what you're comparing is (mostly) the optimization levels
provided by a couple of different compilers, and P-code is generally
used primarily as an intermediate language, at least for the
performance critical parts of the code. The P-code does have an
effect though -- an intermediate language that can't represent all
the concepts involved directly and easily can make it more difficult
to produce good output code.

There are theoretical arguments in favor of dynamic compilation, and
a few of them even work out in reality. For example, the virtual
machine can know the exact type of processor in use, and tailor the
code it generates specifically for that processor. When a developer
compiles C++, he generally won't know the exact type of processor in
the end-user's machine, so he can't select it as specifically.

Realistically, this only makes a substantial difference in a few
cases though. The most obvious (at least right now) is that you can
compile code that can run on either a 32-bit or a 64-bit virtual
machine. If the user has a 64-bit processor and a 64-bit VM
installed, they'll get better performance than if it was running as
32-bit code.

In the other direction, the end user is waiting while a JIT compiler
runs. As a result, the emphasis with (at least most) JIT compilers is
the compiler itself running fast, not that it produces the best
possible output code.

At least in my testing, C++ nearly always beats Java and .NET. The
margin varies from nearly nonexistent to quite large, but seems to
average around 25-30%. On the rare occasion that Java or .NET has
won, the margin has been much smaller, never exceeding 10%.

Other people have run tests with different results (there are a lot
of web sites devoted to proving that Java is faster than C++). So
far, those that have provided tests that were open to examination,
however, quickly showed that the C++ they were using was far from
optimal, in some cases in ways that seemed like they almost _had_ to
be intentional. Just for example, one I looked at years ago allocated
a block of memory with calloc, which zeros the memory, then
immediately used memset to zero the memory _again_ -- and then
overwrote that with other data, so there was no reason to zero it at
all. Of course, that's not the only example, but it is fairly
typical.

Conclusions:
1) It probably won't make a huge difference either way.
2) For pure speed, well written C++ still usually wins.
3) You should probably run some benchmarks yourself, not trust
anybody else's (even mine).
 
V

Victor Bazarov

Dan said:
I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?

One is more different than then other.
I am concerned with the speed of executing arithmetic algorithms.

Aren't we all.
-
I have always heard that C++ compiles into machine code which is close
to optimal

You'd have to define "close".
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.

Ask in a C# newsgroup: microsoft.public.dotnet.languages.csharp (or any
other NG with 'csharp' in the name). There is more to P-code (actually
CIL) than meets the eye. For one, CIL is yet again *compiled* by the
CLR into machine language...

V
 
B

Bas

My experience is that dynamic memory allocation in general is (a lot) faster
in C# then with C++.
Computations in C++ are a lot faster.

The first 'problem' you can overcome by writing your own new/delete
operators, but then you must be sure of what you are doing.
In general its more the way you use C++. One can make a program run faster
in C++ than in C# I think, but I think you have to be
more than a moderate C++ programmer.

It's not the language but merely the runtime environment that makes the
program fast. Microsoft has given the runtime environment (.net)
a lot of attention.

If your program doesn't allocate a lot of objects but arithmetic is the
major issue, I would go for C++, specially if your program is not very large
and object orientation
is of minor importance. For example allocate the objects global ( not on the
heap but on the stack I mean), and you win a lot of speed.

Bas from Holland
 
R

Rui Maciel

Dan said:
I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which is close
to optimal
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.


It may not be "apples to apples" but, for a quantitative comparison, you can check
out this benchmark pairing up C# Mono with C++ GNU g++:


http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=csharp&lang2=gpp


Hope this helps,
Rui Maciel
 
B

Balog Pal

Bas said:
My experience is that dynamic memory allocation in general is (a lot)
faster in C# then with C++.
Computations in C++ are a lot faster.

The first 'problem' you can overcome by writing your own new/delete
operators,

Or rather stick to C/C++ style instead of imitating Java. In native C++ code
you don't have so many allocations as the mass of objects sit in stack
frames.
but then you must be sure of what you are doing.
In general its more the way you use C++. One can make a program run faster
in C++ than in C# I think, but I think you have to be
more than a moderate C++ programmer.

Yeah, if there is a measured bottleneck in the program, C++ is more likely
to support alternatives to work it around. But it needs qualified personnel
too.

In general I didn;t see measuring performance so often, many programs run
just compiled with unoptimized settings or with excess logging/debug info
too. Without anyone care. Ability to ship on time is probably a much
greater factor. (exceptions obvoiusly apply)
It's not the language but merely the runtime environment that makes the
program fast. Microsoft has given the runtime environment (.net)
a lot of attention.

Also many programs on windows spend most of the time calling the USER API or
COM controls -- that is not differently done from other programs. "real"
processing time being negligible.
 
J

James Kanze

I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?

There isn't any. There may be (likely will be?) a difference in
the execution times of two actual programs, written in the
different language, and compiled with two concrete
implementations, but until they've been written and compiled,
there isn't any execution to measure, and the results of the the
compilation depend on the compiler.
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which
is close to optimal however C# is converted into p-code. So
the speed question could be, how does p-code compare to
machine code.

p-code? I haven't heard of anything using p-code for at least
20 years.
 
J

jamm

Dan said:
I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which is close
to optimal
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.

Thank you
Dan

c++ is the way to go. The speed difference depends exactly on what algorithm
is in question. But, assuming talented programmers, either language can be
made to perform well. Its all machine code in the end.
 
Ö

Öö Tiib

I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which is close
to optimal
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.

In both C# and C++ it is possible to make rather well-performing
applications. Best implementation in C++ is only maybe 2 times faster
than best in C#. Since current desktop is very powerful that matters
only with rare problems like scientific computations that run for
hours or for real-time automatics/robotics.

Problems with C++: C++ is more complex than closest contenders so it
is harder to learn, simpler to misuse its features and then to drown
in bugs or suboptimal code. All seem to hate it and especially its
platform independence with passion. Core compiler makers are acting
oddly: MS uses number of tricks to make people to use non-standard
extensions or not to compile best that MS compiler can compile from C+
+ code. Intel's runtime library detects that the code runs on AMD and
behaves sub-optimally there. Open source GNU compiler makers seem to
hate Windows platform. The C++ standard committee can not vote a
simple thing like 'thread' into language or its standard library for
decades. Also the developers preferring other languages like C, java
or C# constantly bash C++ on various sites (including wikipedia)
totally pointlessly. No one cares to defend C++ since it still sits
too strongly in industry.
 
B

Brian

In both C# and C++ it is possible to make rather well-performing
applications. Best implementation in C++ is only maybe 2 times faster
than best in C#. Since current desktop is very powerful that matters
only with rare problems like scientific computations that run for
hours or for real-time automatics/robotics.

Problems with C++: C++ is more complex than closest contenders so it
is harder to learn, simpler to misuse its features and then to drown
in bugs or suboptimal code. All seem to hate it and especially its
platform independence with passion.

There are a lot of people who love C++. I'm not sure what
I'd do if I couldn't program using C++. It would be a sad
day.

Core compiler makers are acting
oddly: MS uses number of tricks to make people to use non-standard
extensions or not to compile best that MS compiler can compile from C+
+ code. Intel's runtime library detects that the code runs on AMD and
behaves sub-optimally there.

Resorting to tricks is nothing new, but is unfortunate and
speaks volumes about the companies. Rebbe Nachman said,
"All the world is just a narrow bridge; the most important
thing is not to be afraid." Many people/companies realize
the truth of the world being a narrow bridge and they
become afraid and then they do something stupid.
Open source GNU compiler makers seem to
hate Windows platform. The C++ standard committee can not vote a
simple thing like 'thread' into language or its standard library for
decades.

I don't think it has been decades, but it has been a long
time since an update has been accomplished. Perhaps with
Concepts removed the new standard will be finished by 2012.


Brian Wood
http://webEbenezer.net
(651) 251-9384
 
F

Frank Steinmetzger

Dan here ! schrob:
I must select a language for a compute intensive project.
What is the execution speed difference between C++ and C# ?
I am concerned with the speed of executing arithmetic algorithms.
-
I have always heard that C++ compiles into machine code which is close
to optimal
however C# is converted into p-code. So the speed question could be,
how does p-code compare to machine code.

Well, I've read somewhere (heise perhaps) that MS rewrote all the .NET stuff
in Vista into C++, which is where Windows 7 got its boost in speed from.
Nuff said. :p
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
KetoBurn
Top