473,545 Members | 2,627 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

WHy is C# so much slower than c++???

Did I do something wrong? I cross posted this on the dotnet
development group -- sorry if it is a double posting but we are
seriously considering going to c# and this could be a show stopper.

I ran the following C# program and it ran in 9 seconds (give or take 1
sec)

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace DotProduct1Mill ion
{
class Program
{
static void Main(string[] args)
{

double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;
Console.WriteLi ne(" {0}", System.DateTime .Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
Console.WriteLi ne(" {0}", System.DateTime .Now);
}
}

}

I ran the following C++ program -- it ran in under 1 second. Is C#
really that much slower than C++????
What is the problem here?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc,char *argv[])
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

cout << time(NULL) << endl;

for (long i = 0; i < 2000000000; i++)
{

d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
cout << time(NULL) << endl;

Nov 1 '06 #1
12 1838
okay -- chnaged some code a bit:
double d1 = 0.92;
double d2 = 1.34;
double d3 = 1.0;
Console.WriteLi ne(" {0}", System.DateTime .Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
if (d1 1.0e10) d1 = 9.0;
if (d2 9e10) d2 = 1.6;
This makes the times 12.6 secs for C++ and 15-16 seconds for C#.
Much more reasonable.
I suspect there was an underflow/overflow situation and the C++ code
stopped executing.

Still C# is still ~ 20% slower than C++. Managed C++ too!

jimocz wrote:
Did I do something wrong? I cross posted this on the dotnet
development group -- sorry if it is a double posting but we are
seriously considering going to c# and this could be a show stopper.

I ran the following C# program and it ran in 9 seconds (give or take 1
sec)

using System;
using System.Collecti ons.Generic;
using System.Text;

namespace DotProduct1Mill ion
{
class Program
{
static void Main(string[] args)
{

double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;
Console.WriteLi ne(" {0}", System.DateTime .Now);
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
Console.WriteLi ne(" {0}", System.DateTime .Now);
}
}

}

I ran the following C++ program -- it ran in under 1 second. Is C#
really that much slower than C++????
What is the problem here?

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc,char *argv[])
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

cout << time(NULL) << endl;

for (long i = 0; i < 2000000000; i++)
{

d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
cout << time(NULL) << endl;
Nov 1 '06 #2
This makes the times 12.6 secs for C++ and 15-16 seconds for C#.
Much more reasonable.
I suspect there was an underflow/overflow situation and the C++ code
stopped executing.

Still C# is still ~ 20% slower than C++. Managed C++ too!
Hi,
enable the optimizer in your C# project settings. It is disabled by default.
Then the C++.NET and C# results should be nearly identical.
Native C++ will probably a bit faster dues to lower memory overhead.

For more accurate timing you should use the StopWatch class, and not include
the Console.WriteLi ne statements in your time measurements. That will give
you ms accuracy or better.

As far as switching to C# goes: from personal experience I can say that
application development times and debugging times are much shorted in C#
than in native C++ or even C++.NET.
YMMV.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Nov 2 '06 #3


for(long i = 0...
in C#
and
for(long i = 0...
in C++ are not the same loops. A long in C# is 64 bit while in C++ it's a
32bit value.
Make i an int and you'll see that both will run at the same speed.
Willy.

"jimocz" <dw****@gmail.c omwrote in message
news:11******** **************@ k70g2000cwa.goo glegroups.com.. .
| okay -- chnaged some code a bit:
| double d1 = 0.92;
| double d2 = 1.34;
| double d3 = 1.0;
| Console.WriteLi ne(" {0}", System.DateTime .Now);
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| if (d1 1.0e10) d1 = 9.0;
| if (d2 9e10) d2 = 1.6;
|
|
| This makes the times 12.6 secs for C++ and 15-16 seconds for C#.
| Much more reasonable.
| I suspect there was an underflow/overflow situation and the C++ code
| stopped executing.
|
| Still C# is still ~ 20% slower than C++. Managed C++ too!
|
|
|
| jimocz wrote:
| Did I do something wrong? I cross posted this on the dotnet
| development group -- sorry if it is a double posting but we are
| seriously considering going to c# and this could be a show stopper.
| >
| I ran the following C# program and it ran in 9 seconds (give or take 1
| sec)
| >
| using System;
| using System.Collecti ons.Generic;
| using System.Text;
| >
| namespace DotProduct1Mill ion
| {
| class Program
| {
| static void Main(string[] args)
| {
| >
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
| Console.WriteLi ne(" {0}", System.DateTime .Now);
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| Console.WriteLi ne(" {0}", System.DateTime .Now);
| }
| }
| >
| }
| >
| I ran the following C++ program -- it ran in under 1 second. Is C#
| really that much slower than C++????
| What is the problem here?
| >
| #include <iostream>
| #include <cstdlib>
| #include <ctime>
| >
| using namespace std;
| >
| int main(int argc,char *argv[])
| {
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
| >
| cout << time(NULL) << endl;
| >
| for (long i = 0; i < 2000000000; i++)
| {
| >
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| cout << time(NULL) << endl;
|
Nov 2 '06 #4
Hi,
enable the optimizer in your C# project settings. It is disabled by
default.
Then the C++.NET and C# results should be nearly identical.
Native C++ will probably a bit faster dues to lower memory overhead.

For more accurate timing you should use the StopWatch class, and not
include the Console.WriteLi ne statements in your time measurements. That
will give you ms accuracy or better.
In addition, you might try running the loop once to warm up and JIT the
code, and then do your test once it has been fully JITd, since you're probly
seeing a certain amount of "warm-up" time while the IL code gets compiled
and it makes things looks slower (for such a small program its probly not
noticeable but still)... as well, run it RELEASE mode and not DEBUG mode...

You might also want to try changing the code to:

static void Main(string[] args)
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;
Console.WriteLi ne(" {0}", System.DateTime .Now);

unchecked
{
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
}
Console.WriteLi ne(" {0}", System.DateTime .Now);
}

By default the numerical operations are _checked_ which means the the
runtime will check for overflows. Changing it to unchecked in C# eliminates
that extra check and puts it on par with what the C++ code is actually
doing. You can change it in the project also (globally) but the code above
is just fine. I prefer to unchecked {...} my code where needed for the sake
of clarity when reading the code later.
Thanks,
Shawn
Nov 3 '06 #5
Actually, executing the code in checked {...} vs. unchecked {...} really
doesn't make a whole lot of difference. However, running it in DEBUG (with
or without debugging) vs. RELEASE makes a huge difference.

For example, running in DEBUG with debugging takes 380 seconds, DEBUG
without debugging takes 56 seconds, and RELEASE without debugging takes 14
seconds.

The first iteration includes the JIT time and the second iterations is
already JIT'd.
Thanks,
Shawn


using System;
using System.Collecti ons.Generic;
using System.Text;

namespace ConsoleApplicat ion1
{
class Program
{
private static void Main(string[] args)
{

for (int i = 0; i < 2 ; i++)
{
Console.WriteLi ne("Iteration {0} of 2", (i + 1));

Test1();
Test2();
}
Console.ReadLin e();
}

private static void Test1()
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

System.Diagnost ics.Stopwatch watch = new System.Diagnost ics.Stopwatch() ;
watch.Start();

checked
{
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
}

watch.Stop();

Console.WriteLi ne("Test1: " + watch.Elapsed.T otalSeconds.ToS tring());
}

private static void Test2()
{
double d1 = 0.727272;
double d2 = 0.26252;
double d3 = 343432.232;

System.Diagnost ics.Stopwatch watch = new System.Diagnost ics.Stopwatch() ;
watch.Start();

unchecked
{
for (long i = 0; i < 2000000000; i++)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
}
}

watch.Stop();

Console.WriteLi ne("Test2: " + watch.Elapsed.T otalSeconds.ToS tring());
}

}
}
Nov 3 '06 #6
As I said in another reply, you should make the loop counter an int instead
of a long (2000000000 fits in an int), incrementing a long (64 bit value in
C#) is more expensive than incrementing an int on a 32 bit CPU.
Also, when comparing C# performance against C++, you should keep in mind
that a long in C++ != long in C#.

Willy.

"Shawn B." <le****@html.co mwrote in message
news:ev******** ******@TK2MSFTN GP03.phx.gbl...
| Actually, executing the code in checked {...} vs. unchecked {...} really
| doesn't make a whole lot of difference. However, running it in DEBUG
(with
| or without debugging) vs. RELEASE makes a huge difference.
|
| For example, running in DEBUG with debugging takes 380 seconds, DEBUG
| without debugging takes 56 seconds, and RELEASE without debugging takes 14
| seconds.
|
| The first iteration includes the JIT time and the second iterations is
| already JIT'd.
|
|
| Thanks,
| Shawn
|
|
|
|
| using System;
| using System.Collecti ons.Generic;
| using System.Text;
|
| namespace ConsoleApplicat ion1
| {
| class Program
| {
| private static void Main(string[] args)
| {
|
| for (int i = 0; i < 2 ; i++)
| {
| Console.WriteLi ne("Iteration {0} of 2", (i + 1));
|
| Test1();
| Test2();
| }
|
|
| Console.ReadLin e();
| }
|
| private static void Test1()
| {
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
|
| System.Diagnost ics.Stopwatch watch = new System.Diagnost ics.Stopwatch() ;
| watch.Start();
|
| checked
| {
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| }
|
| watch.Stop();
|
| Console.WriteLi ne("Test1: " + watch.Elapsed.T otalSeconds.ToS tring());
| }
|
| private static void Test2()
| {
| double d1 = 0.727272;
| double d2 = 0.26252;
| double d3 = 343432.232;
|
| System.Diagnost ics.Stopwatch watch = new System.Diagnost ics.Stopwatch() ;
| watch.Start();
|
| unchecked
| {
| for (long i = 0; i < 2000000000; i++)
| {
| d3 = d1 * d2;
| d1 = d3 * d2;
| d2 = d2 * d2;
| d3 = d1 * d2;
| d3 = d3 * d2;
| }
| }
|
| watch.Stop();
|
| Console.WriteLi ne("Test2: " + watch.Elapsed.T otalSeconds.ToS tring());
| }
|
| }
| }
|
|
Nov 3 '06 #7
Okay,
I am running in release mode and I have changed the longs to doubles as
seen here:
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main(int argc,char *argv[])
{
double d1 = 0.92;
double d2 = 1.34;
double d3 = 1.0;
double i;

float now = clock()/(float)CLOCKS_P ER_SEC;
for (i = 0; i < 6000000000.0; i+= 1.0)
{

d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
if (d1 1.0e10) d1 = 9.0;
if (d2 9e10) d2 = 1.6;

}
cout << "i = " << i << " Time to run CPP ==" <<
(clock()/(float)CLOCKS_P ER_SEC - now) << endl;

}
AND the C# code ::
using System;
using System.Collecti ons.Generic;
using System.Text;
using System.Diagnost ics;

namespace DotProduct1Mill ion
{
class Program
{

static void Main(string[] args)
{
Stopwatch sw;
double d1 = 0.92;
double d2 = 1.34;
double d3 = 1.0;

sw = new Stopwatch();
sw.Start();

for (double i = 0; i < 6000000000.0; i+= 1.0)
{
d3 = d1 * d2;
d1 = d3 * d2;
d2 = d2 * d2;
d3 = d1 * d2;
d3 = d3 * d2;
if (d1 1.0e10) d1 = 9.0;
if (d2 9e10) d2 = 1.6;

}
sw.Stop();
TimeSpan ts = sw.Elapsed;
string s = String.Format(" {0:00}:{1:00}.{ 2:000}",
ts.Minutes, ts.Seconds,
ts.Milliseconds );

Console.WriteLi ne("Time with C# = {0}", s);
}
}
}

I have included the if statements to avoid any underflow/overflow
stuff.

The interesting thing is if I turn off optimization the C# code runs
faster. (81 secs to 121 secs)
However, if I turn on optimization the C++ code runs in 45 secs and C#
drops to 61 secs. I guess the C++ compiler is smarter than the C#
compiler!?

Thanks to all for the suggestions and help.

Jim

Nov 3 '06 #8
The interesting thing is if I turn off optimization the C# code runs
faster. (81 secs to 121 secs)
However, if I turn on optimization the C++ code runs in 45 secs and C#
drops to 61 secs. I guess the C++ compiler is smarter than the C#
compiler!?
Be aware that you are comparing apples and oranges.
IF you would compare C# with a C++/CLI application, they would be nearly
identical.
Number crunching is almost always going to be faster in a native
application.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Nov 3 '06 #9
You are right on -- I compiled the C++ as managed code (I assume that
is CLI) and it was around 62 seconds.

This begs the question why is managed code so much slower than native?
I know you JIT compile it (that shouldn't affect the times since the
compile time is not included in the time) but is running in the virtual
machine following JIT 30% slower?

I suppose this is a major reason to maintain native C++ code --
computations.

Bruno van Dooren [MVP VC++] wrote:
The interesting thing is if I turn off optimization the C# code runs
faster. (81 secs to 121 secs)
However, if I turn on optimization the C++ code runs in 45 secs and C#
drops to 61 secs. I guess the C++ compiler is smarter than the C#
compiler!?

Be aware that you are comparing apples and oranges.
IF you would compare C# with a C++/CLI application, they would be nearly
identical.
Number crunching is almost always going to be faster in a native
application.

--

Kind regards,
Bruno van Dooren
br************* *********@hotma il.com
Remove only "_nos_pam"
Nov 3 '06 #10

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

Similar topics

114
9696
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
12
2437
by: Gustavo L. Fabro | last post by:
Greetings! Getting straight to the point, here are the results of my experiment. I've included my comments and questions after them. The timing: (The total time means the sum of each line's drawing time. Time is measured in clock ticks (from QueryPerformanceCounter() API). The processor resolution (QueryPerformanceFrequency()) for my
14
2341
by: Roy Gourgi | last post by:
Hi, How much is C# slower than C++? TIA Roy
5
1437
by: Michael | last post by:
i experience slower compile times with VC++ 2003 compared to VC+6.0. Anyone experiencing the same? Should that be expected? This ineed matters, when total compilation time is > 1h and you have to wait 10-50% longer...
87
4896
by: John Rivers | last post by:
Hello everybody, I just wondered if anybody else has noticed this? It takes around 6 seconds to start debugging a very simple ASPX page with VS.NET whereas VB6 takes under 0.5 seconds, even with very large and complex projects. This is a real shame :(
4
2089
by: dhnriverside | last post by:
Hi peeps I'm having some problems with my Session State sticking (it keeps resetting itself) - I haven't looked into it yet, but I was wondering about using SQL Server as an out of process state manager? I've heard it's slower - and I was just wondering... how much slower? (this is for an Intranet app) Cheers
77
5161
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop. Is this because .NET is inherently slower or does the C# compiler merely produce code that is not as well optimized as the C++ compiler?
25
3229
by: news.microsoft.com | last post by:
Hi all, First post here. I'm porting an application I wrote in VB6, over to VB.NET 2005. It could be said I'm really struggling with some (most!) of the syntax of VB.NET 2005, but I'm getting there. I'm relying a lot, at this point, on sample code from Microsoft, and other forums about the Internet. One thing I've painfully noticed...
0
7428
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
7941
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...
1
7452
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7784
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5354
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
5071
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
3467
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1039
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.