Unmanaged Code slower than Managed Code!

Discussion in 'C#' started by Frankie-T, Feb 12, 2009.

  1. Frankie-T

    Frankie-T New Member

    Joined:
    Feb 12, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi Experts,
    I'm trying to get into unmanaged DLLs and implementing them into C#-programs.
    The reason is simply to enhance the performance of a algorithm.

    So, getting into the basics I tried to test the speed by creating a for-loop, which counts up to a high value and returns a boolean parameter.
    Strangley the for-loop in the C#-Form runs faster, than the loop inside the dll (round 10% faster, I'd say).

    Here's the code:


    Code:

    DLL
    Code:
    // DLL_Test_1.cpp : Definiert den Einstiegspunkt für die DLL-Anwendung.
    //
    #include "stdafx.h"
    #ifdef _MANAGED
    #pragma managed(push, off)
    #endif
     
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
          )
    {
        return TRUE;
    }
    extern "C" __declspec(dllexport) int Rechnen(int Zahl1, int Zahl2);
    extern "C" __declspec(dllexport) bool forSchleife(int hoehe);
     
    int Rechnen(int Zahl1, int Zahl2)
    {
     int Zahl3 = Zahl1+Zahl2;
     return (Zahl3);
    }
    bool forSchleife(int hoehe)
    {
     int i;
     bool fertig = false;
     
     for(i = 0; i < hoehe; i++)
     {
      int testZahl = i;
     }
     
     fertig = true;
     return (fertig);
    }
     
    #ifdef _MANAGED
    #pragma managed(pop)
    #endif
    
    Form
    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    namespace DLL_Benutzen
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button2_Click(object sender, EventArgs e)
            {
                textBox4.Text = "---------";
                Boolean Ready = DLLTest.forSchleife(1000000000);
                textBox4.Text = Ready.ToString();
            }
            private void button3_Click(object sender, EventArgs e)
            {
                textBox5.Text = " ";
                Boolean managedReady = forSchleifeManaged(1000000000);
                textBox5.Text = managedReady.ToString();
            }
            public Boolean forSchleifeManaged(int hoehe)
            {
                Boolean managedFertig = false;
                for (int i = 0; i < 1000000000; i++)
                {
                    int test = i;
                }
                managedFertig = true;
                return (managedFertig);
            }
     
        }
        static class DLLTest
        {
            [DllImport(@"DLL_Test_1.dll")]
            public static extern bool forSchleife(int hoehe);
        }
    }
    

    Thanks for your help.
    Frankie-T
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    This is very much a possibility and I would not disagree but the speed is not only determined for running the for loop.

    First why you get unexpected results as per your requirements is better use of memory and cache by the managed code.

    The speed of the program should be done from the time loader starts loading the program into memory to exit of the program and there you would see the difference.

    C# programs I have seen load faster second time than first time and so now you can safely assume why.
     
  3. Frankie-T

    Frankie-T New Member

    Joined:
    Feb 12, 2009
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,
    thats disapointing.

    See, my problem is, that I have to fill a buffer with an image.
    It's huge (about 2000x5000px) and therefor it takes a while to fill the buffer.

    My idea was to bring the filling of the buffer into unmanaged code.

    Any idea how I could realize that?



    Frankie-T
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Unless compelled try avoiding mixture of managed and un-managed code.

    If you have some well tested un-managed code use it but do not try to develop both of them as switching can kill many things and specially time.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice