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
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

