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

Home Posts Topics Members FAQ

30% difference in speedof C# vs C++ for math?

I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:

double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
}

It takes 2300ms in VB.NET, 1000ms in C# but only 700ms in VC++

Does this sound right?

Thanks.
Richard
Nov 17 '05 #1
21 2817
did you build in release version?
did you open the optimizing parameter in the building process?
is the VC++ code native or managed?

all of these will affect the performance.

Nov 17 '05 #2

<re*****@gmail. com> wrote in message
news:11******** **************@ f14g2000cwb.goo glegroups.com.. .
did you build in release version?
did you open the optimizing parameter in the building process?
is the VC++ code native or managed?

all of these will affect the performance.


1. Both were run in Release mode (C# and VC++).
2. The VC++ program was optimized for speed. I could not find a similar
option for C#
3. The VC++ code is native.

Is there an option to maximize speed in a compiled C# application?

Richard
Nov 17 '05 #3
Richard,

If you need that kind of speed, buy a math component (and one
particular to your type of application) and use C# to develop the GUI
and business logic. That makes far more sense than writing everything
in C++.

Best Regards
Johann Blake

Nov 17 '05 #4
"Johann Blake" <jo*********@ya hoo.com> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.com.. .
Richard,

If you need that kind of speed, buy a math component (and one
particular to your type of application) and use C# to develop the GUI
and business logic. That makes far more sense than writing everything
in C++.

Best Regards
Johann Blake


C++ is OK for me, I have been using it for 15 years. But I thought I would
gain some type of advantage by switching to C#

Well apparently not, because I'm not sure if I would want to do something
crazy like:

ClumsyThirdPart yMathLibrary clumsy = new ClumsyThirdPart yMathLibrary();
double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a =
clumsy.Multply( clumsy.Add(clum sy.Subtract(clu msy.Divide(clum sy.Add(clumsy.S u
btract(clumsy.D ivide(clumsy.Mu ltiply(clumsy.M od(n, 5) * 2), 3) 4), 6), 3),
n), n), 2);
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
}

I couldn't even finish the first line. I've got just 11,000 more to go.
C# should be faster. One shouldn't have to resort to clumsy stuff like that.

Richard
Nov 17 '05 #5
Richard,

I should have made my point much clearer. You don't use a third party
math library to do basic math. The library should contain the math
functions pertinent to what you want to accomplish. For example, if you
need to run a DSP FFT algorithm on an analog signal, you use a DSP
library with a FFT function.

Since you already know C++, write these functions yourself in C++ and
call them from C#. That is what I am doing at the moment. All my "math"
stuff is in C++ but the actual GUI and business logic is C#.

Developing GUI and business logic in C# makes far more sense and has
far more advantageous than doing it in C++.

Johann

Nov 17 '05 #6
On Wed, 3 Aug 2005 22:19:41 -0700, "Rich" <no@spam.invali d> wrote:
I couldn't even finish the first line. I've got just 11,000 more to go.
C# should be faster. One shouldn't have to resort to clumsy stuff like that.


Who said it "should" be faster? Santa Claus?

Different languages are better at different things. C# is great at
things like speed of development, clarity of code, runtime linking,
memory management. C++ is great at things like hardware access,
porting to many different systems, and raw speed.

Your experience is the same as mine, by the way. In numerical code,
C# is only 50-80% the speed of C++. That's how it is, for now. But
(Visual) C++ is a fairly stable language that has had many years of
gradual optimizer improvements while C# and .NET are still getting a
ton of new features with each release (example: generics in 2.0,
embedded database queries planned for 3.0). Once the Microsoft teams
start focusing on the optimizer numerical code might improve, too.
--
http://www.kynosarges.de
Nov 17 '05 #7
Rich,

a) Your code does not really test floating point arithmetic. Since variable
n and all the constants used are integers, you're only doing integer
arithmetic there...

b) If you're comfortable with C++, maybe you should consider using Visual
C++ 2005. It offers most of the productivity-enhancement tools that there
are in VS 2005 for C#, and it's the only language that allows you to mix
native and managed code. This will allow you to recompile your
math-intensive code as native, if the tests show that managed code is
significantly slower than native, with minimum effort.

Regards - Octavio

"Rich" <no@spam.invali d> escribió en el mensaje
news:evdIe.5506 1$4o.50065@fed1 read06...
I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:

double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
}

It takes 2300ms in VB.NET, 1000ms in C# but only 700ms in VC++

Does this sound right?

Thanks.
Richard

Nov 17 '05 #8
I can't believe there is such a huge difference between VB and C#, there
must be something wrong here.
Mind to post the whole code?

Willy.
"Rich" <no@spam.invali d> wrote in message
news:evdIe.5506 1$4o.50065@fed1 read06...
I was considering C# for developing a scientific application, but I have
noticed a ~30% difference between VC++ .NET and C# on the same machine,
under identical conditions:

double a = 0,b = 0, c = 0, d = 0, e = 0;
for(int n = 0; n != 6000000; n++)
{
a = n % 5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
b = n * 2.3 - 1 *2 / 3 - 4 + 6 / 3 - n + n * 2;
c = n * 3 / 3.5 *2 / 3 - 4 + 6 / 3 - n + n * 2;
d = n + 1 * 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
e = n / 2 *2 / 3 - 4 + 6 / 3 - n + n * 2;
}

It takes 2300ms in VB.NET, 1000ms in C# but only 700ms in VC++

Does this sound right?

Thanks.
Richard

Nov 17 '05 #9
I can't believe there is such a huge difference between VB and C#, there
must be something wrong here.
Mind to post the whole code?


I'm guessing that the VB code uses the / operator which performs
implicit floating point division (as opposed to \), whereas the C#
code just does integer math like Octavio said. That would account for
at least some of the difference.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #10

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

Similar topics

34
7035
by: yensao | last post by:
Hi, I have a hard time to understand difference and similarities between Relational database model and the Object-Oriented model. Can somebody help me with this? Thank you in advance. Yensao
3
9462
by: RobG | last post by:
A little while ago I opined that do/while loops are harder to read than for loops, and therefore I preferred using for loops. However, it was pointed out that do/while has significant performance benefits as evidenced by: <URL:http://www.websiteoptimization.com/speed/10/10-2.html> (There's a bug in the page, testLoop is both a function...
4
15699
by: jamesyreid | last post by:
Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual...
10
5474
by: Bonzol | last post by:
vb.net Hey there, could someone just tell me what the differnce is between classes and modules and when each one would be used compared to the other? Any help would be great Thanx in advance
2
2899
by: Blackmore | last post by:
I am trying to use javascript to calculate the difference between two form inputted dates and return the result to another form object. When I load up the page with the function on my web browser the form does not load and I get a message to say that the page contains errors, presumably as the function is not initialising or being referenced...
4
14250
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for all rows where period_ = 3. The allocation table for info has 210 rows. I have two scripts below. The first script where I specify a period on...
6
2292
by: jason | last post by:
Hi, I learned my lesson about passing pointers, but now I have a question about macros. Why does the function work and the MACRO which is doing the same thing on the surface, does not work in the following small example ? #include <stdio.h>
11
5119
by: sunnyalways4u2000 | last post by:
hello sir, Sir will please tell me the exact difference between C and advanced C...what are the extra features or funcions...etc added in this advanced one.
2
1959
by: RN1 | last post by:
This is how I am calculating the time difference: ------------------------ Function TimeDiff(ByVal StartDateTime, ByVal EndDateTime) Dim h As Integer Dim m As Integer Dim t1 As DateTime Dim t2 As DateTime Dim ts As TimeSpan
0
7656
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
7805
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
7416
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
5969
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
5325
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
4944
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
3449
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
1878
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
0
701
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.