Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speed up math code in C# by writing a C dll?

I have a very large nested for loop in which some multiplications and additions are performed on floating point numbers.

for (int i = 0; i < length1; i++)
{
    double aa = 0;
    for(int h = 0; h < 10; h++)
    {
       aa += omega[i][outsideGeneratedAddress[h]];
    }

    double alphaOld = alpha;
    alpha = Math.Sqrt(alpha * alpha + aa * aa);

    s = -aa / alpha;
    c = alphaOld / alpha;

    for(int j = 0; j <= i; j++)
    {
        double oldU = u[j];
        u[j] = c * oldU + s * omega[i][j];
        omega[i][j] = c * omega[i][j] - s * oldU;
    }
}

This loop is taking up the majority of my processing time and is a bottleneck.

Would I be likely to see any speed improvements if I rewrite this loop in C and interface to it from C#?

EDIT: I updated the code to show how s and c are generated. Also the inner loop actually goes from 0 to i, though it probably doesn't make much difference to the question

EDIT2: I implemented the algorithm in VC++ and linked it with C# through a dll and saw a 28% speed boost over C# when all optimisations are enabled. The argument to enable SSE2 works particularly well. Compiling with MinGW and gcc4.4 only gave a 15% speed boost. Just tried the Intel compiler and saw a 49% speed boost for this code.

like image 343
Projectile Fish Avatar asked May 25 '10 01:05

Projectile Fish


People also ask

Is C faster than C sharp?

Performance: C++ is widely used when higher level languages are not efficient. C++ code is much faster than C# code, which makes it a better solution for applications where performance is important.

Why C Plus Plus is fast?

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.


2 Answers

Updated:

What happens if you write inner loop to take account of locality of reference:

for (int i = 0; i < length1; i++) 
{ 
    s = GetS(i); 
    c = GetC(i); 
    double[] omegaTemp = omega[i]; 

    for(int j = 0; j < length2; j++) 
    { 
        double oldU = u[j]; 
        u[j] = c * oldU + s * omegaTemp[j]; 
        omegaTemp[j] = c * omegaTemp[j] - s * oldU; 
    } 
} 
like image 152
Mitch Wheat Avatar answered Oct 04 '22 10:10

Mitch Wheat


Use an unsafe block and pointers to index into your omega array. This will remove the overhead of range checking and may be a significant win if you do enough accesses. A lot of time may also be being spent in your GetS() and GetC() functions, which you didn't provide source for.

like image 21
Donnie Avatar answered Oct 04 '22 12:10

Donnie