How close can I get C # for C ++ performance for small intensive tasks?

I was thinking about the difference in speed from C ++ to C #, mainly about compiling C # into byte code, which is accepted by the JIT compiler (is that right?) And all C # checks.

I notice that you can disable many of these functions, both in the compilation options and possibly with the unsafe keyword, since unsafe code cannot be verified using the common language runtime.

Therefore, if you wrote a simple console application in both languages, which deployed an imaginary coin infinitely many times and displayed the results on the screen every 10,000 and so on, how much would there be a difference in speed? I chose this because it is a very simple program.

I would like to check this out, but I don't know C ++ or I have tools to compile it. This is my version of C #, though:

 static void Main(string[] args) { unsafe { Random rnd = new Random(); int heads = 0, tails = 0; while (true) { if (rnd.NextDouble() > 0.5) heads++; else tails++; if ((heads + tails) % 1000000 == 0) Console.WriteLine("Heads: {0} Tails: {1}", heads, tails); } } } 

Is the difference sufficient to intentionally compile sections of code "unsafe" or into DLLs that do not have some compilation options, such as overflow checking? Or does this happen in a different way, where it would be useful to compile partitions in C ++? I am sure that the speed of interaction also comes into play.

To avoid subjectivity, I repeat the specific parts of this question:

  • Does C # have performance improvements when using unsafe code?
  • Do compilation options such as disabling overflow checking, performance improvements, and do they affect insecure code?
  • Will the program above be higher in C ++ or casually different?
  • Is it worth compiling long intensive crunching tasks in a language such as C ++, or is it used / unsafe for a bonus? Less subjective, can I perform an intensive operation faster by doing this?
+4
source share
3 answers

The given example is erroneous because it does not show the real use of both programming languages. Using simple data types to measure language speed will not bring anything interesting. Instead, I suggest you create a template class in C ++ and compare it with what is possible in C # for generic classes. As a result, the objects will lead to some important results, and you will see that C ++ is faster than C #. Not to mention that you are comparing a lower-level programming language with C #.

Does C # Increase Performance Using Insecure Code?

Yes, he will have an incentive, but it is not recommended to write only code with insecure. Here's why: Code written using an insecure context cannot be verified as safe, so it will only be executed when the code is fully trusted. In other words, unsafe code cannot be executed in an untrusted environment. For example, you cannot run unsafe code directly from the Internet. http://msdn.microsoft.com/en-us/library/aa288474(VS.71).aspx

Will the program above be higher in C ++ or is it carelessly different?

Yes, the program will be a little faster in C ++. C ++ is a lower programming language and even faster if you start using a library of algorithms (random_shuffle comes to mind).

Is it worth it to compile long intensive number-crunchy tasks in a language such as C ++ or use / unsafe for a bonus? Less subjective, can I do an intensive operation faster by doing this?

It depends on the project ...

+2
source

Up to 100% speed - a lot depends on the task, just said.

More than 100% - yes, because the compiler knows your processor exactly on time, and I doubt that you are really optimizing your hardware platform;)

The lack of SSE is a problem if you are performing matrix operations.

Do a few things with lots of arrays (image manipulation) Testing arrays kills you, but pointers work (that is, unsafe code) as they get around this.

For things like overflow checking, be careful. Like in: in C ++, you have the same thing. If you need an overflow check, there is no performance issue;)

In most cases, I personally would not worry about C ++. Partly yes, especially if you can use SSE:

So, in the end, a lot depends on NATURE, if your calculations.

+1
source

I would recommend taking a look at this question: Is it possible to compile .NET IL code into machine code?

0
source

Source: https://habr.com/ru/post/1307446/


All Articles