I'm looking into developing an application that will process data from a line-scan camera at around 2000 lines (frames) per second. For this real-time application, I feel that C/C++ are the way to go. (It is my feeling, and others will agree that Managed code just isn't right for this task.)
However, I've done very little MFC, or any other C++ GUI. I am really getting to do C# GUIs very well, though.
So it seems natural to me to write the data-intensive code in C/C++, and the GUI in C#. The GUI will be used for set-up/calibration/on-line monitoring (and possibly outputting of data via UDP, because it's easier in C#.
So first, I'd like to see if anyone agrees that this would be the way to go. Based on my programming experience (good at low-level C algorithms, and high-level C# GUI design), it just feels right.
Secondly, I'm not sure the right way to go about it. I just threw together a solution in VS2005, which calls some (extern "C") DLL functions from a C# app. And to make sure I could do it, I wrote to some global variables in the DLL, and read from them:
test.h
int globaldata; extern "C" __declspec(dllexport) void set(int); extern "C" __declspec(dllexport) int get();
test.cpp
extern int data=0; __declspec(dllexport) void set(int num) { data = num; } __declspec(dllexport) int get() { return data; }
test.cs
[DllImport("test")] private static extern void set(int num); [DllImport("test")] private static extern int get();
Calling get()
and set()
work properly (get()
returns the number that I passed to set()
).
Now, I know that you can export a C++ class as well, but does it have to be managed? How does that work? Am I going about this the right way?
Thanks for all your help!
*** EDIT ***
First of all, THANK YOU for your fantastic answers so far! I'm always incredibly impressed with Stack Overflow...
I guess one thing I should have hit on more, was not necessarily raw speed (this can be prototyped and benchmarked). One thing that has me more concerned is the non-deterministic behavior of the Garbage Collector. This application would not be tolerant of a 500ms delay while performing garbage collection.
I am all for coding and trying this in pure C#, but if I know ahead of time that the GC and any other non-deterministic .NET behavior (?) will cause a problem, I think my time would be better spent coding it in C/C++ and figuring out the best C# interface.
C is not always faster. C is slower than, for example Modern Fortran. C lets pointer aliasing happen, which means some good optimizations are not possible. Particularly when you have multiple execution units, this causes data fetch stalls.
Performance-based on Nature Of Language C++ language is an object-oriented programming language, and it supports some important features like Polymorphism, Abstract Data Types, Encapsulation, etc. Since it supports object-orientation, speed is faster compared to the C language.
The programs that you write in C compile and execute much faster than those written in other languages. This is because it does not have garbage collection and other such additional processing overheads. Hence, the language is faster as compared to most other programming languages.
There is no reason that you can't write high performance code entirely in C#.
Performance (C# Programming Guide)
Rico Mariani's Performance Blog (an excellent resource)
Tuning .NET Application Performance
SO questions on the same/similiar topic:
Other articles:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With