Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am an experienced C# developer, and I just started asking myself something.
I've read a lot about differences between C++ and C# in game development.
Almost everyone said that C++ is better in game dev because it can directly access memory through a pointer. As far as I know, C# can also use pointers if the unsafe keycode is used.
So, that means that C# can also directly access memory. And then the question comes again, what is the difference between the C++ pointer and the C# pointer? Is one better then the other? If there are no differences, why would C++ be better than C#?
(I know from my own experience that I've had problems with the C# garbage collection, so I thought that this might be the reason C++ is preferred)
I'll say that the weak point of C# is that, given a void* pointer, you can't always cast it to a MyStruct* pointer, and surely as hell you can't cast it to a MyStruct[] or a byte[] (and the array type is one of the basic types of .NET, and is used pretty much everywhere). This makes interop quite difficult and slow, because often you have to first copy from a void* to a newly created MyStruct[] just to be able to use the data in .NET.
The alternative clearly is working everywhere with pointers in C# (you can probably do it) and minimize the use of arrays [], but the languages isn't built for that. For example the generic subsystem (List<T>) doesn't accept pointers (you can't List<int*>). You can clearly use IntPtr... but then you have to cast it to int* when you need a int*... it is a pain.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I was sticking on Unity for a while, without knowing much about Computer Science/Performance management.
Now, I took some C++ experience, with some concerns on IO/performance/etc some "Computer Engineering" stuffs.
Then I got back to my old Unity project, which had no concern about those thing at all, and found out that there is ref(or out) keyword for call-by-reference, as I could do in my C++ project.
Is it common to use ref(for call-by-reference purpose) of C# on 'business level'?(maybe game companies with Unity?)
If you come from C++ then you have this thought in your head that you need to pass references, because if you don't, the whole object will be copied and most likely that is bad (tm).
In C#, types are either reference types or value types. Reference types are passed by reference automatically. If you pass a class, it can be null, so it must already be a reference to the actual class instance (or in C++ terminology a "pointer", because references in c++ cannot be null).
All classes are reference types. You don't need ref or out for performance reasons ever. If you find yourself using ref or out for performance reasons on a struct, that should be your clue that it should be a class instead.
So no, using ref or out is not common in C# at all. It is only used sparingly, in the places where you would not get the desired result without it. Those keywords are not meant for performance considerations.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I read this article: https://medium.com/microsoft-open-source-stories/how-microsoft-rewrote-its-c-compiler-in-c-and-made-it-open-source-4ebed5646f98
Since C# has a built in garbage collector, is Roslyn slower than the previous compiler which was written in C++? Did they perform any benchmarks?
Let me address a question that you didn't explicitly ask but applies to your question.
Question: Is explicit garbage collection faster than implicit garbage collection?
Answer: As you may already know C++/C uses explicit garbage collection which means that free() must be called to deallocate memory allocated on the heap. On the other hand, C# uses implicit garbage collection which means the memory on the heap is deallocated in the background. The key here is implicit garbage collection will deallocate memory when needed at optimal times while explicit will always deallocate each object individually(if done correctly). Implicit garbage collection achieves this by communicating with the OS and by using some other algorithms. In all, in most situations, implicit garbage collection will perform better than explicit due to the above explanation. For more info check out this post.
Answer To Your Question: Because I have not seen any bench marks myself, it is almost impossible to say if one would be faster than the other for sure. There are many other features than garbage collection which would effect the speed of each langauge implementation. To clarify, C# is a bytecode based language that uses the JIT(Just-In-Time) compiler. If I had to choose, I would choose the C++ implementation to be faster due to the JIT optimizations lacking in some cases compared to the C++ compiler. Again, when it comes to how fast these two languages will perform it will depend on the situation. For example, there are some optimizations that JIT can preform that are impossible to do with the C++ compiler.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I want to use a c++ project to do some calculations for a c# project and return the results.
I was wondering if I would benefit a more efficient calculation speed in c++ if I do so?
Would still be efficient if I wrapped the native code in c++/cli?
Are there any examples out there?
Just as simple example say you have two double values A and B in C#, how would you have c++ project to receive A and B and a string value "plus" or "times" to calculate and return A + B or A * B?
Use Process.Start(); to spawn your optimized program. You will be able to pass parameters and even read the output. Start here: https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx
You've got two separate issues in your question: "How do I", and "Should I".
If you're having problems with the "How do I", please post a question with the specific code you have, and what problems you're having.
"Should I" is somewhat of a nebulous question: It depends a lot on the type of calculations you're trying to do. These questions often have no one right answer. (Also note that this type of question is often offtopic for Stack Overflow for that very reason, so this question may be closed.)
For some types of calculations, the C++ compiler might produce more efficient code than the .Net Jitter. For some types, it won't make a difference. C++ would also let you do things like using the GPU to perform the calculations.
Also, consider how long it will take you to write this optimized code, and how often you're going to run it. If this needs to run overnight once a month, maybe a couple hours to run is fine.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
This article spells out some reasons F#'s performance is occasionally better than C#. It says in it's "Firstly" section that only F# generates tail calls.
What exactly does that mean? And why is it a performance boost? This one thing may actually make or break between F# and C# for my chess app, which uses a ton of recursion.
Performance will depend more on the way you implement your program than the language. F# may generate IL better for some things while the C# compiler will be better for others. When choosing the languages you should consider other things rather than just performance.
If you're writing your chess program to learn F#, give it a try, it's an awesome language, just don't expect super blazing fast programs just because you're using a functional language.
Edit to answer the new question:
The F# compiler indeed does generate IL that has the tail. op code whareas the C# compiler doesn't. That by itself doesn't make F# faster or more performatic than C#, as you can see in my original answer above, but can indeed make a difference in your specific chess app, since you are stating that recursion is heavily used.
As a side note, the CLR may generate some simpler tail call optimizations during runtime, so for simpler functions in a x64 enviroment, even IL generated by the C# compiler may have tail call optimization.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have the source code for a C library that I need to use with my C# ASP.Net application. It sounds like the way to handle this would be to create a C++ dll wrapper and DLLImport that into my C# project. I have found this to be kind of ugly as far as when it throws an exception the entire application crashes. Is this the best way to accomplish this, or is there a safer way? I am trying to do some research on how I might be able to make a COM object with the C library, but haven't really found much there. Is it possible to make a COM object and reference it as .NET managed code?
I googled my way around this. I found this great article comparing
a C# facade using with PInvoke and a lot of marshalling
a Facade into the interop layer in C++/CLI and compiling in mixed mode for consumption by C#.
Also, this MSDN citation : C++ Interop is recommended over explicit PInvoke because it provides better type safety, is typically less tedious to implement, is more forgiving if the unmanaged API is modified, and makes performance enhancements possible that are not possible with explicit PInvoke.
Also your idea(C++ COM facade) seems legit as preached by the good book with 2 advantages:
The resulting classes can be used from languages other than Visual C++.
The details of the COM interface can be hidden from the managed client code. .NET data types can be used in place of native types, and the details of data marshaling can be performed transparently inside the custom runtime callable wrappers.