Speed of C++ DLL from C#

C++ DLL faster than C# code?

Page 1 of 1

6 Replies - 6073 Views - Last Post: 10 July 2009 - 10:29 AM Rate Topic: -----

#1 logBase2  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 27-December 08

Speed of C++ DLL from C#

Posted 08 July 2009 - 10:06 AM

Hi everyone,
I want to do some audio programming but have heard C# might be a bit too slow for low level things (?). However, if I code the low level aspects in C++ and call the DLL from the C# application, would the functions execute the speed of the C++ version, or will it still be as slow as if I had created it in C# since it still has the .NET overheads?
Thanks
Is This A Good Question/Topic? 0
  • +

Replies To: Speed of C++ DLL from C#

#2 Amrykid  Icon User is offline

  • 4+1=Moo
  • member icon

Reputation: 150
  • View blog
  • Posts: 1,589
  • Joined: 16-December 08

Re: Speed of C++ DLL from C#

Posted 08 July 2009 - 05:20 PM

View PostlogBase2, on 8 Jul, 2009 - 10:06 AM, said:

Hi everyone,
I want to do some audio programming but have heard C# might be a bit too slow for low level things (?). However, if I code the low level aspects in C++ and call the DLL from the C# application, would the functions execute the speed of the C++ version, or will it still be as slow as if I had created it in C# since it still has the .NET overheads?
Thanks

Well.....C# is technically compiled twice. The first time when its compiling to IL and the second is when the J-I-T(Just In Time) compiler compiles it to machine language. Machine language is the fastest for a machine. C++ is directly compiled to machine language. But to answer your question, C++ would be faster than its C# counter-part. Calling it from C# would "call" the function at high-level language speed, but execute the C++ code as "C++" speed. But coding everything in C++ would, overall, be fastest (execution speed wise). Any experts or mods correct me if I'm wrong.

This post has been edited by Amrykid: 08 July 2009 - 05:25 PM

Was This Post Helpful? 0
  • +
  • -

#3 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: Speed of C++ DLL from C#

Posted 08 July 2009 - 09:46 PM

What about VC++.Net 200X?
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Speed of C++ DLL from C#

Posted 08 July 2009 - 10:04 PM

This is really a native vs managed code question. Native code will usually run faster than managed code. In some cases the managed code will run close to the same speed and in other instances faster. It is a depends on the case thing. In some cases managed code has been optimized to do a task faster than native code does the same task.
Was This Post Helpful? 0
  • +
  • -

#5 logBase2  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 27-December 08

Re: Speed of C++ DLL from C#

Posted 09 July 2009 - 10:35 AM

Thank you for the replies everyone.

I have much more experience in C# than C++, so would prefer to use that. However, speed is important in the audio processing stage and so C++ would be advantageous.

Quote

What about VC++.Net 200X?


Are you asking me, or is that a suggestion? If the latter, than I'll put forward the question :D . Would it be recommendable to use VC++ .Net instead of unmanaged C++ if I'm more familiar with C#? Or, since it has to compile twice anyway, any speed gain would be reduced by being managed?

I'm still yet to actually code the aspects, so I can't test the differences, but from what I've heard, C++ is preferred when extracting features from audio.

This post has been edited by logBase2: 09 July 2009 - 10:35 AM

Was This Post Helpful? 0
  • +
  • -

#6 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,565
  • Joined: 05-June 09

Re: Speed of C++ DLL from C#

Posted 09 July 2009 - 10:46 AM

Well, chances are that, depending on which library you use, C# will be faster. I've found C# to be faster than C++ surprisingly in calls to certain (most) WINAPI (DllImport'd) functions within a loop...

It's probably going to be up to you, the difference won't be noticable either way I'm sure. If the .NET Framework has classes that you can use to obtain your result, I'd stick with them for sure.

Using VC++.NET (ie. C++/CLR) will be slower than C# due to it's switching between native and managed mode (which is almost inevitable since you'll probably learn from native C++ tutorials).

This post has been edited by MageUK: 09 July 2009 - 10:47 AM

Was This Post Helpful? 1
  • +
  • -

#7 logBase2  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 14
  • Joined: 27-December 08

Re: Speed of C++ DLL from C#

Posted 10 July 2009 - 10:29 AM

Thanks for the advice, MageUK. I'll think about whether it's worth me coding it in C# or pulling in an already created DLL.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1