C # memory optimization for large arrays

Here are two pieces of code in C ++ and C # that do exactly the same thing:

C ++
http://ideone.com/UfL5R

#include <stdio.h> int main(int argc, char *argv[]) { char p[1000000]; unsigned int i,j; unsigned long long s=0; for(i=2;i<1000000;i++) p[i]=1; for(i=2;i<500000;) { for(j=2*i;j<1000000;j+=i) p[j]=0; for(i++;!p[i];i++); } for(i=3,s=2;i<1000000;i+=2) if(p[i]) s+=i; printf ("%lld\n",s); return 0; } 

time: 0.01s memmory: 2576 kB

WITH#
http://ideone.com/baXYm

 using System; namespace ConsoleApplication4 { internal class Program { private static void Main(string[] args) { var p = new byte[1000000]; ulong i, j; double s = 0; for(i=2;i<1000000;i++) p[i]=1; for(i=2;i<500000;) { for(j=2*i;j<1000000;j+=i) p[j]=0; for(i++;p[i]==0;i++); } for(i=3,s=2;i<1000000;i+=2) if(p[i]!=0) s+=i; Console.WriteLine(s); } } } 

time: 0.05s mem: 38288 kB

How can I improve C # code to prove that C # can be as fast as C ++ for my colleague?

As you can see, C # runtime is 5 times longer and memory consumption is 15 times longer.

+4
source share
5 answers

How to MORE increase the performance of your C # code

Go to "unsafe" (unmanaged) for this ... every time you do someSortOfArray[i] , the .NET platform does all sorts of neat things (like checking outside borders) that take time.

This is really the whole point of unmanaged (and then using pointers and doing myPointer ++).

Just to clarify, if you leave unmanaged and then do a for-loop and do someArray[i] , you haven't saved anything.

Another S.O. question that may help you: True Unsafe Code Performance

Renouncement

By the way, I do not say to do this all the time, but rather as an answer only for this particular question.

+4
source

Compile and run in release mode. I get exactly 0.01 from the C # version when building and running in Release mode. As for memory consumption, you are comparing apples to oranges. A managed environment will consume more memory because it hosts the CLR and the garbage collector, which do not come without cost.

+9
source

How can I improve C # code to prove that C # can be as fast as C ++ for my colleague?

You can not. There are legitimate areas where C ++ is significantly faster than C #. But there are also areas where C # code will work better than equivalent C ++ code. These are different languages ​​with different strengths and weaknesses.

But as a programmer, you really have to base your decisions in logic.

Logic dictates that you must first gather information and then decide based on this.

You, on the contrary, made a decision first, and then searched for information to support it. This may work if you are a politician, but this is not a good way to write software.

Don't go hunting for evidence that C # is faster than C ++. Instead, check which option is faster in your case.

In any case, if you want to prove that X can be as fast as Y, you should do it in the usual way: make X as fast as Y. And as always, when performing performance tuning, the profiler is your best friend. Find out exactly where the extra time is spent, and then find out how to fix it.

Memory usage is a lost reason..NET just uses more memory for several reasons:

  • it has a large runtime library that must be present in the process address space
  • .NET objects have additional members not present in C ++ classes, so they use more memory
  • The garbage collector means that, as a rule, you will have a certain amount of "no longer used, but not recovered" memory lying around. In C ++, memory is usually freed immediately. In .NET, this is not so ..NET is based on the assumption that memory is cheap (which is usually true).
+6
source

Just mark your time. Its not shown how you measured the runtime. At startup, you can expect reasonable overhead for .NET applications. Therefore, if you want only the runtime of the loops, you must start the inner loops several (many) times, skip the first iterations 1..2, measure the other iterations and calculate the average value.

I expect the results to be more similar. However, as always, when aiming at "maximum performance", precautions regarding memory management are important. Here, probably, it would be enough to prevent the β€œnew” inside the measurement functions. Reuse p [] at each iteration.

+3
source

Memory usage may be related to garbage collection. In Java, memory usage is heavy - garbage collection only happens when you need more memory. This is for speed reasons, so it makes sense that C # does the same. You should not do this in the release code, but to show the large memory that you are actually using, you can call GC.Collect() before measuring the memory usage. Do you really care how much memory it uses? It seems speed is more important. And if you have memory limits, you can probably set the amount of memory that your program will use before garbage collection.

+1
source

All Articles