Connecting Tech Pros Worldwide Forums | Help | Site Map

Performance in csharp, scientific simulation

Michael Gorbach
Guest
 
Posts: n/a
#1: Nov 17 '05
I was asked this summer to write a monte carlo code to simulation
magnetic nanoparticles. For the nonphysicists, basicly it is a
simulation where most of the time is taken up by looping through each
pair in an array of 500 or so particles, in order to calculate
interaction potential. I wrote what i have so far in csharp because i
wanted to learn it and though it would give me some good experience. I
am now beginning to understand that .net and managed code in general
lags far behind performance wise. Eventually i am probably going to
have to port to c++, unmanaged, because the simulation code will need
to run on linux as well.
My question is this:
What can i do in my chsarp code right now to speed up the performance.
The main method that is run hundreds of times a second basicly involves
calculating a vector dot product (using my own vector class), and an
exponential. Would marking the method unsafe speed anything up>


AlexS
Guest
 
Posts: n/a
#2: Nov 17 '05

re: Performance in csharp, scientific simulation


I believe before jumping to conclusions you must profile your application.
Aside from memory use you can find out which methods are taking most of time
/ calls and check if code is optimal in there.

As you might know, ideal performance is achieved by fetching result by
argument. Simple iteration is not always the best approach. This includes
names :-)

HTH
Alex

"Michael Gorbach" <mgorbach@gmail.com> wrote in message
news:1122061394.644523.297090@f14g2000cwb.googlegr oups.com...[color=blue]
> I was asked this summer to write a monte carlo code to simulation
> magnetic nanoparticles. For the nonphysicists, basicly it is a
> simulation where most of the time is taken up by looping through each
> pair in an array of 500 or so particles, in order to calculate
> interaction potential. I wrote what i have so far in csharp because i
> wanted to learn it and though it would give me some good experience. I
> am now beginning to understand that .net and managed code in general
> lags far behind performance wise. Eventually i am probably going to
> have to port to c++, unmanaged, because the simulation code will need
> to run on linux as well.
> My question is this:
> What can i do in my chsarp code right now to speed up the performance.
> The main method that is run hundreds of times a second basicly involves
> calculating a vector dot product (using my own vector class), and an
> exponential. Would marking the method unsafe speed anything up>
>[/color]


AlexS
Guest
 
Posts: n/a
#3: Nov 17 '05

re: Performance in csharp, scientific simulation


I believe before jumping to conclusions you must profile your application.
Aside from memory use you can find out which methods are taking most of time
/ calls and check if code is optimal in there.

As you might know, ideal performance is achieved by fetching result by
argument. Simple iteration is not always the best approach. This includes
names :-)

HTH
Alex

"Michael Gorbach" <mgorbach@gmail.com> wrote in message
news:1122061394.644523.297090@f14g2000cwb.googlegr oups.com...[color=blue]
> I was asked this summer to write a monte carlo code to simulation
> magnetic nanoparticles. For the nonphysicists, basicly it is a
> simulation where most of the time is taken up by looping through each
> pair in an array of 500 or so particles, in order to calculate
> interaction potential. I wrote what i have so far in csharp because i
> wanted to learn it and though it would give me some good experience. I
> am now beginning to understand that .net and managed code in general
> lags far behind performance wise. Eventually i am probably going to
> have to port to c++, unmanaged, because the simulation code will need
> to run on linux as well.
> My question is this:
> What can i do in my chsarp code right now to speed up the performance.
> The main method that is run hundreds of times a second basicly involves
> calculating a vector dot product (using my own vector class), and an
> exponential. Would marking the method unsafe speed anything up>
>[/color]


Michael Gorbach
Guest
 
Posts: n/a
#4: Nov 17 '05

re: Performance in csharp, scientific simulation


how do i go about doing this application profiling? im more or less new
to serious programming so any help would be appreaciated.
Also, what do you mean by "fetching result by argument?"

Are there are references on performance you could suggest?

Michael Gorbach
Guest
 
Posts: n/a
#5: Nov 17 '05

re: Performance in csharp, scientific simulation


how do i go about doing this application profiling? im more or less new
to serious programming so any help would be appreaciated.
Also, what do you mean by "fetching result by argument?"

Are there are references on performance you could suggest?

Peter Bromberg [C# MVP]
Guest
 
Posts: n/a
#6: Nov 17 '05

re: Performance in csharp, scientific simulation


MIchael, in most testing scenarios the performance of well written (and
I stress "well - written") C# and C++ is comparable. Both eventually
are run as machine code. In the case of C#, where intensive math
computations are being made, performance can be increased by the
judicious and careful use of pointer - based arithmetic. Alex's
comment about profiling is right on the money, especially if this is a
new language you are just learning.

Compuware has an excellent freeware profiler (a "Community" edition)
and there are others. These can help you tremendously.

Peter Bromberg [C# MVP]
Guest
 
Posts: n/a
#7: Nov 17 '05

re: Performance in csharp, scientific simulation


MIchael, in most testing scenarios the performance of well written (and
I stress "well - written") C# and C++ is comparable. Both eventually
are run as machine code. In the case of C#, where intensive math
computations are being made, performance can be increased by the
judicious and careful use of pointer - based arithmetic. Alex's
comment about profiling is right on the money, especially if this is a
new language you are just learning.

Compuware has an excellent freeware profiler (a "Community" edition)
and there are others. These can help you tremendously.

AlexS
Guest
 
Posts: n/a
#8: Nov 17 '05

re: Performance in csharp, scientific simulation


I would suggest to have a look at math books dealing with optimal
algorithms.

My comment is about most efficient computation, which is not always
achievable. y = f(x). X is argument. F - some function. If you can deliver y
for any given x, for example, using some table, you probably won't be able
to create anything more efficient in terms of speed. But you will pay with
memory use.

That's why I suggest profiling. You can start with also free MS CLRProfiler,
which you can download at
http://www.microsoft.com/downloads/d...DisplayLang=en
Source code is quite good in demonstrating some of common optimization
techniques, by the way

HTH
Alex

"Michael Gorbach" <mgorbach@gmail.com> wrote in message
news:1122072676.834908.42780@z14g2000cwz.googlegro ups.com...[color=blue]
> how do i go about doing this application profiling? im more or less new
> to serious programming so any help would be appreaciated.
> Also, what do you mean by "fetching result by argument?"
>
> Are there are references on performance you could suggest?
>[/color]


AlexS
Guest
 
Posts: n/a
#9: Nov 17 '05

re: Performance in csharp, scientific simulation


I would suggest to have a look at math books dealing with optimal
algorithms.

My comment is about most efficient computation, which is not always
achievable. y = f(x). X is argument. F - some function. If you can deliver y
for any given x, for example, using some table, you probably won't be able
to create anything more efficient in terms of speed. But you will pay with
memory use.

That's why I suggest profiling. You can start with also free MS CLRProfiler,
which you can download at
http://www.microsoft.com/downloads/d...DisplayLang=en
Source code is quite good in demonstrating some of common optimization
techniques, by the way

HTH
Alex

"Michael Gorbach" <mgorbach@gmail.com> wrote in message
news:1122072676.834908.42780@z14g2000cwz.googlegro ups.com...[color=blue]
> how do i go about doing this application profiling? im more or less new
> to serious programming so any help would be appreaciated.
> Also, what do you mean by "fetching result by argument?"
>
> Are there are references on performance you could suggest?
>[/color]


Christoph Nahr
Guest
 
Posts: n/a
#10: Nov 17 '05

re: Performance in csharp, scientific simulation


Last I checked CLR Profiler does *not* profile speed but only memory
usage. If the simulation is using fixed arrays, as I suspect, it
won't give any results.

Besides, the question was which is faster for this task -- C# or C++?
Just profiling C# evidently won't give any answer to this question.

On Fri, 22 Jul 2005 22:11:56 -0400, "AlexS"
<salexru2000NO@SPAMsympaticoPLEASE.ca> wrote:
[color=blue]
>I would suggest to have a look at math books dealing with optimal
>algorithms.
>
>My comment is about most efficient computation, which is not always
>achievable. y = f(x). X is argument. F - some function. If you can deliver y
>for any given x, for example, using some table, you probably won't be able
>to create anything more efficient in terms of speed. But you will pay with
>memory use.
>
>That's why I suggest profiling. You can start with also free MS CLRProfiler,
>which you can download at
>http://www.microsoft.com/downloads/d...DisplayLang=en
>Source code is quite good in demonstrating some of common optimization
>techniques, by the way
>
>HTH
>Alex[/color]
--
http://www.kynosarges.de
Christoph Nahr
Guest
 
Posts: n/a
#11: Nov 17 '05

re: Performance in csharp, scientific simulation


Last I checked CLR Profiler does *not* profile speed but only memory
usage. If the simulation is using fixed arrays, as I suspect, it
won't give any results.

Besides, the question was which is faster for this task -- C# or C++?
Just profiling C# evidently won't give any answer to this question.

On Fri, 22 Jul 2005 22:11:56 -0400, "AlexS"
<salexru2000NO@SPAMsympaticoPLEASE.ca> wrote:
[color=blue]
>I would suggest to have a look at math books dealing with optimal
>algorithms.
>
>My comment is about most efficient computation, which is not always
>achievable. y = f(x). X is argument. F - some function. If you can deliver y
>for any given x, for example, using some table, you probably won't be able
>to create anything more efficient in terms of speed. But you will pay with
>memory use.
>
>That's why I suggest profiling. You can start with also free MS CLRProfiler,
>which you can download at
>http://www.microsoft.com/downloads/d...DisplayLang=en
>Source code is quite good in demonstrating some of common optimization
>techniques, by the way
>
>HTH
>Alex[/color]
--
http://www.kynosarges.de
Christoph Nahr
Guest
 
Posts: n/a
#12: Nov 17 '05

re: Performance in csharp, scientific simulation


If I understand you correctly, you already have a working C# program,
and you don't do any tricky stuff that might be hard to port.

So the solution is very simple: get a C++ compiler, convert your
program to C++ (or even plain C if you can turn your vector into a
simple struct), and compare the execution times for both program
versions. There you have your result -- no profiler needed.

Whether the C++ version is faster will depend mostly on how good the
C++ compiler's optimizer is. Numerical code can be optimized very
well, but .NET doesn't do any of those optimizations. This can lead
to substantial performance benefits for unmanaged code -- assuming, of
course, that your C++ compiler actually does such optimizations.

As for speeding up the C# code...

Merely *marking* a method unsafe does nothing at all. This keyword
merely *allows* you to use pointer operations which *might* be faster,
but that's not guaranteed either.

Using pointers to address array elements might speed up C#, but if
you're iterating over an array the range checks are already optimized
away by the JIT compiler. Also, make sure that C# overflow checking
is disabled -- I think it's off with /optimize but I'm not sure.

Microsoft has a free C/C++ compiler download somewhere on MSDN but
since your code must run on Linux you'll probably use gcc anyway.
--
http://www.kynosarges.de
Christoph Nahr
Guest
 
Posts: n/a
#13: Nov 17 '05

re: Performance in csharp, scientific simulation


If I understand you correctly, you already have a working C# program,
and you don't do any tricky stuff that might be hard to port.

So the solution is very simple: get a C++ compiler, convert your
program to C++ (or even plain C if you can turn your vector into a
simple struct), and compare the execution times for both program
versions. There you have your result -- no profiler needed.

Whether the C++ version is faster will depend mostly on how good the
C++ compiler's optimizer is. Numerical code can be optimized very
well, but .NET doesn't do any of those optimizations. This can lead
to substantial performance benefits for unmanaged code -- assuming, of
course, that your C++ compiler actually does such optimizations.

As for speeding up the C# code...

Merely *marking* a method unsafe does nothing at all. This keyword
merely *allows* you to use pointer operations which *might* be faster,
but that's not guaranteed either.

Using pointers to address array elements might speed up C#, but if
you're iterating over an array the range checks are already optimized
away by the JIT compiler. Also, make sure that C# overflow checking
is disabled -- I think it's off with /optimize but I'm not sure.

Microsoft has a free C/C++ compiler download somewhere on MSDN but
since your code must run on Linux you'll probably use gcc anyway.
--
http://www.kynosarges.de
Steve Walker
Guest
 
Posts: n/a
#14: Nov 17 '05

re: Performance in csharp, scientific simulation


In message <1122061394.644523.297090@f14g2000cwb.googlegroups .com>,
Michael Gorbach <mgorbach@gmail.com> writes[color=blue]
>Eventually i am probably going to
>have to port to c++, unmanaged, because the simulation code will need
>to run on linux as well.[/color]

Not necessarily; look at http://www.mono-project.com

--
Steve Walker
Michael Gorbach
Guest
 
Posts: n/a
#15: Nov 17 '05

re: Performance in csharp, scientific simulation


Thanks everyone for the great responses. I love this newsgroup!
Steve, yes I do know about mono and i will use it if i dont port, but
its speed is questionable. At best, it will run as fast as microsoft
..net, at worst there will be a performance hit.
I will take a look at both profilers that have been suggested. Thanks
for the references.
My question is to Peter: What exactly is pointer-based arithmitic and
where can find algorithms/books or other help on the subject? I think
this maybe the best short term solution to my problem. Also, i may
think about using a table to look up the exponential function values.

Steve Walker
Guest
 
Posts: n/a
#16: Nov 17 '05

re: Performance in csharp, scientific simulation


In message <1122300034.553506.321160@z14g2000cwz.googlegroups .com>,
Michael Gorbach <mgorbach@gmail.com> writes[color=blue]
>Thanks everyone for the great responses. I love this newsgroup!
>Steve, yes I do know about mono and i will use it if i dont port, but
>its speed is questionable. At best, it will run as fast as microsoft
>.net, at worst there will be a performance hit.[/color]

Sniffing round the net, Mono currently appears to be a little slower
than Microsoft's implementation, but I'd expect the gap to close.

--
Steve Walker
Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#17: Nov 17 '05

re: Performance in csharp, scientific simulation


Michael Gorbach <mgorbach@gmail.com> wrote:[color=blue]
> Thanks everyone for the great responses. I love this newsgroup!
> Steve, yes I do know about mono and i will use it if i dont port, but
> its speed is questionable. At best, it will run as fast as microsoft
> .net, at worst there will be a performance hit.[/color]

That depends on what you do with it. When some colleagues were
investigating performance comparisons, they found that for many things
..NET was faster than Mono, but for some others Mono was faster than
..NET.

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Dov
Guest
 
Posts: n/a
#18: Nov 17 '05

re: Performance in csharp, scientific simulation


I am learning C# and I am writing scientific software. What I did
find so far is that when the data is in arrays, the differences between
C# and the equivalent C++/C codes are very small.

I tried (1) a Monte-Carlo simulation, and (2) translated from C a
routine that solves a system of linear equations (simq from the CEPHES
package) and benchmarked a large system with the coefficients
initialized by random number generator. The C/C++ & C# versions ended
with the same result and nearly the same time.

However, when I translated part of the Stepanov benchmark ( a C++
benchmark that measures abstraction penalty ) I did find a major
performance hit. I suspect that the current C# optimizer is currently
not yet developed enough to deal with high level of abstraction. Of
course, I am a C# beginner and perhaps I can learn to translate better.

I think that if you are a little careful in performance-critical
parts of your code, you can achieve nearly C speeds.

Dov

Michael Gorbach wrote:[color=blue]
> I was asked this summer to write a monte carlo code to simulation
> magnetic nanoparticles. For the nonphysicists, basicly it is a
> simulation where most of the time is taken up by looping through each
> pair in an array of 500 or so particles, in order to calculate
> interaction potential. I wrote what i have so far in csharp because i
> wanted to learn it and though it would give me some good experience. I
> am now beginning to understand that .net and managed code in general
> lags far behind performance wise. Eventually i am probably going to
> have to port to c++, unmanaged, because the simulation code will need
> to run on linux as well.
> My question is this:
> What can i do in my chsarp code right now to speed up the performance.
> The main method that is run hundreds of times a second basicly involves
> calculating a vector dot product (using my own vector class), and an
> exponential. Would marking the method unsafe speed anything up>[/color]

Dov Bai
Guest
 
Posts: n/a
#19: Nov 17 '05

re: Performance in csharp, scientific simulation


I am learning C# and I am writing scientific software. What I did
find so far is that when the data is in arrays, the differences between

C# and the equivalent C++/C codes are very small.


I tried (1) a Monte-Carlo simulation, and (2) translated from C a
routine that solves a system of linear equations (simq from the CEPHES
package) and benchmarked a large system with the coefficients
initialized by random number generator. The C/C++ & C# versions ended
with the same result and nearly the same time.


However, when I translated part of the Stepanov benchmark ( a C++
benchmark that measures abstraction penalty ) I did find a major
performance hit. I suspect that the current C# optimizer is currently
not yet developed enough to deal with high level of abstraction. Of
course, I am a C# beginner and perhaps I can learn to translate better.



I think that if you are a little careful in performance-critical
parts of your code, you can achieve nearly C speeds.


Dov


Michael Gorbach wrote:[color=blue]
> I was asked this summer to write a monte carlo code to simulation
> magnetic nanoparticles. For the nonphysicists, basicly it is a
> simulation where most of the time is taken up by looping through each
> pair in an array of 500 or so particles, in order to calculate
> interaction potential. I wrote what i have so far in csharp because i
> wanted to learn it and though it would give me some good experience. I
> am now beginning to understand that .net and managed code in general
> lags far behind performance wise. Eventually i am probably going to
> have to port to c++, unmanaged, because the simulation code will need
> to run on linux as well.
> My question is this:
> What can i do in my chsarp code right now to speed up the performance.
> The main method that is run hundreds of times a second basicly involves
> calculating a vector dot product (using my own vector class), and an
> exponential. Would marking the method unsafe speed anything up>[/color]

Michael Gorbach
Guest
 
Posts: n/a
#20: Nov 17 '05

re: Performance in csharp, scientific simulation


My particles are stored in a class Nanoparticle, which contains a
member of a Vector class i created. The dot product operator is done by
the * operator which i overloaded for the Vector class. It simply uses
a for loop over the coordinates to do the dot product.

Dov Bai
Guest
 
Posts: n/a
#21: Nov 17 '05

re: Performance in csharp, scientific simulation


And that is probably the source of your slowdown - you have created at
least two levels of abstraction above your particles: (1) The Vector
class, and (2) overloading the Vector operations (e.g. the * operator).

Dov

Michael Gorbach
Guest
 
Posts: n/a
#22: Nov 17 '05

re: Performance in csharp, scientific simulation


What is the simplest way to do that without created this slowdown?

Dov Bai
Guest
 
Posts: n/a
#23: Nov 17 '05

re: Performance in csharp, scientific simulation


Where performance is critical, try to use built-in data structures. For
example, if you have 3D particles use two dimensional jagged arrays
(e.g. double [][] ) to store the coordinates. The size of the first
dimension is the number of particles and of the the second dimension is
3 (for x, y, z).

Dov

Michael Gorbach wrote:[color=blue]
> What is the simplest way to do that without created this slowdown?[/color]

Closed Thread


Similar C# / C Sharp bytes