473,385 Members | 1,757 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Use of C-dll, an optimization?

I've got a C# program that uses the algorithm for Levenstein Distance.
(C-source at http://www.merriampark.com/ldc.htm) This function is
called _very_ frequently, and uses up lots of cpu time. Therefore Im
interested in any optimizations that would speed things up a bit.

Would there be anyting to gain by using the algorthim as a compiled
C-dll in my C# program? I figure there would be some additional
overhead in the DLL handling, and I'd have to add unicode->ansi
conversion (Marshal.StringToHGlobalAnsi()) on both of the argument
strings.

Any ideas are very much appreciated.

Thanks!

Nov 16 '05 #1
4 2167

Hi Vitling:

Since you are frequently calling the function I suspect the interop
overhead might actually slow you down - but measuring your application
in your environment is the only way to tell for sure.

Perhaps you could post the C# code in a short but complete program and
we could make some suggestions.

--
Scott
http://www.OdeToCode.com

On Wed, 07 Jul 2004 12:59:04 GMT, mrboo@ht_u.se (Vitling) wrote:
I've got a C# program that uses the algorithm for Levenstein Distance.
(C-source at http://www.merriampark.com/ldc.htm) This function is
called _very_ frequently, and uses up lots of cpu time. Therefore Im
interested in any optimizations that would speed things up a bit.

Would there be anyting to gain by using the algorthim as a compiled
C-dll in my C# program? I figure there would be some additional
overhead in the DLL handling, and I'd have to add unicode->ansi
conversion (Marshal.StringToHGlobalAnsi()) on both of the argument
strings.

Any ideas are very much appreciated.

Thanks!


Nov 16 '05 #2

"Daniel Jin" <Da*******@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...
pinvoke itself certainly has overhead. and in addition, I've read that unicode->ansi conversion is very expensive. any performance you might gain
from going unmanaged probably will be negated.
I took a look at the C code, it seems simple enough that I don't see why managed code can't do a decent job. the only additional cost you will incur
is the bound checks on array access. some of which might even be jitted
away. you can avoid all bound checks using unsafe code, stackalloc the int
array, access string with a char*.
like scott said, only way to see the performance is by testing. do both unsafe and pinvoke and see which is faster.

Definitely. However, I'm going to guess that the C# implementation would be
quicker. Here's why:

n=strlen(s);
This is O(n) in C, O(1) for Framework strings.

d=malloc((sizeof(int))*(m+1)*(n+1));
malloc is significantly slower than a Framework allocation. Of course,
there's a price to be paid in garbage collection, but that will be done
later and anyway a gen.0 collect is very fast.

int minimum(int a,int b,int c)
As it stands this function probably won't be inlined in C. I'd expect it to
be in C# though.

Everything else
Will be JIT-compiled in C# so I wouldn't have thought there'd be a
noticeable difference.

On top of that, you'll have a DLL/interop overhead if you go via the C
route. The string unicode <-> ansi issue could be overcome using wide-chars.

However I'm willing to be proved wrong, and as others have said, only
measurement can tell you the actual answer.

(If you do write it in C#, don't hoist the string length checks either -- I
believe the JIT can optimise away bounds checking if you leave them where
they ought to be, in the for statement).

Stu

"Vitling" wrote:
I've got a C# program that uses the algorithm for Levenstein Distance.
(C-source at http://www.merriampark.com/ldc.htm) This function is
called _very_ frequently, and uses up lots of cpu time. Therefore Im
interested in any optimizations that would speed things up a bit.

Would there be anyting to gain by using the algorthim as a compiled
C-dll in my C# program? I figure there would be some additional
overhead in the DLL handling, and I'd have to add unicode->ansi
conversion (Marshal.StringToHGlobalAnsi()) on both of the argument
strings.

Any ideas are very much appreciated.

Thanks!

Nov 16 '05 #3
I too had an opportunity of using a C++ DLL in a C# DLL
and that has too many problems when used with the pinvoke.
I got rid only after making my C# dll a COM+ one.
Nov 16 '05 #4
On Wed, 07 Jul 2004 12:59:04 GMT, mrboo@ht_u.se (Vitling) wrote:
I've got a C# program that uses the algorithm for Levenstein Distance.
(C-source at http://www.merriampark.com/ldc.htm) This function is
called _very_ frequently, and uses up lots of cpu time. Therefore Im
interested in any optimizations that would speed things up a bit.

Would there be anyting to gain by using the algorthim as a compiled
C-dll in my C# program? I figure there would be some additional
overhead in the DLL handling, and I'd have to add unicode->ansi
conversion (Marshal.StringToHGlobalAnsi()) on both of the argument
strings.

Any ideas are very much appreciated.

Thanks!

Thanks for all help. I think Im going to stick to my C# version, since
any performance gain seems to be minor (at best).
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Alex Vinokur | last post by:
For instance, we need to measure performance of assignment 'ch1 = ch2' where ch1 and ch2 are of char type. We need to do that for different optimization levels of the same compiler. Here is...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
21
by: mjbackues at yahoo | last post by:
Hello. I'm having a problem with the Visual Studio .net (2003) C++ speed optimization, and hope someone can suggest a workaround. My project includes many C++ files, most of which work fine...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
1
by: Marcus.CM | last post by:
Hi, I use the following ctype to load a .so library in Linux. vr = ctypes.CDLL(sstr) And the following to release it so that i can reload the library without quiting the python script. ...
2
by: db2admin | last post by:
hi, I have query which runs great when optimization level is changed to 3 but does not run fine with default optimization level of 5. since this is a query in java code, i do not know how can i...
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.