Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fast C++ program, C# GUI, possible? [closed]

Tags:

c++

c#

export

dll

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.

like image 997
Jonathon Reinhart Avatar asked Aug 08 '09 04:08

Jonathon Reinhart


People also ask

Is C the fastest programming language?

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.

Which is faster C or C++?

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.

Why C program is fast?

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.


1 Answers

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:

  • C++ performance vs. Java/C#
  • How much faster is c++ than c#?

Other articles:

  • Microbenchmarking C++, C#, and Java
  • Harness the Features of C# to Power Your Scientific Computing Projects
  • Find Application Bottlenecks with Visual Studio Profiler
  • Debunking C# vs C++ Performance
like image 192
Mitch Wheat Avatar answered Oct 04 '22 11:10

Mitch Wheat