Address: http://www.cnblogs.com/fujingqiu/
Assembly languages have performance advantages, C # has flexibility and organizational structure advantages. The combination of the two is my ideal.
The idea is to use C # For Large components and classes.ProgramUse the Assembly DLL.
You can download the masm32 toolkit from the Internet. The version can be 8.2 or 9.0.
I have compiled a DLL. This function is a summation integer array.
. 386
. Model flat, stdcall
Option Casemap: None
Include \ masm32 \ include \ windows. inc
Include \ masm32 \ include \ kernel32.inc
Include \ masm32 \ include \ user32.inc
Includelib \ masm32 \ Lib \ user32.lib
Includelib \ masm32 \ Lib \ kernel32.lib
. Code
Libmain proc hinstdll: DWORD, reason: DWORD, unused: DWORD
. If reason = dll_process_attach
MoV eax, true
RET
. Endif
RET
Libmain endp
Sumarray proc stdcall point: DWORD, Len: DWORD
Push ESI
Push EBX
MoV ESI, Point
MoV EBX, Len
MoV eax, [esi]
Dec EBX
. If! Zero?
. Repeat
Add ESI, 4
Add eax, [esi]
Dec EBX
. Until zero?
. Endif
Pop EBX
Pop ESI
RET
Sumarray endp
End libmain
I also just learned the compilation. If there are any mistakes, please give me more advice.
After saving as DLL. ASM, click Project> build all. DLL. OBJ has been generated.
Then write the DLL. Def file:
Library DLL
Exports sumarray
Run the command link/subsystem: Windows/dll/DEF: D:/project/ASM/DLL. Def D:/project/ASM/obj in the masm32/bin directory.
In this way, the DLL. dll file is available in the bin directory. (Not very familiar, do not laugh)
Calling this DLL in C # is not complicated.
1. Copy the DLL. DLL to the bin/DEBUG directory.
2. Add using system. runtime. interopservices;
3. Declare external Methods
[Dllimport ("DLL. dll")]
Public static extern int32 sumarray (int32 * point, int32 length );
4. Call
Sum = sumarray (point, source. Length );
5. Because pointers are used, you need to set the class to unsafe;
Public unsafe partial class form1
6. Set the project's unsafe attribute.
I compiled a program to test its performance:
Int [] Source = New Int [ 100000 ];
Random random = New Random ();
For ( Int I = 0 ; I < Source. length; I ++ )
{
This. style. Display = 'none'; codehighlighter0000150_210_open_text.style.display = 'none'; codehighlighter0000150_210_closed_image.style.display = 'inline'; interval = 'inline ';
} "Id =" codehighlighter0000150_210_open_image "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_150_210_open_image.style.display = 'inline'; codehighlighter1_150_210_open_text.style.display = 'inline ';
} "Id =" codehighlighter0000150_210_closed_image "style =" display: none; "> {
Source [I]=Random. Next (100);
}
Int Sum = 0 ;
Long Start = Environment. tickcount;
For ( Int Time = 0 ; Time < 1000 ; Time ++ )
{
This. style. Display = 'none'; codehighlighter%348_495_open_text.style.display = 'none'; codehighlighter%348_495_closed_image.style.display = 'inline'; inline = 'inline ';
} "Id =" codehighlighter0000348_495_open_image "> {
This. style. Display = 'none'; items = 'none'; codehighlighter1_348_495_open_image.style.display = 'inline'; codehighlighter1_348_495_open_text.style.display = 'inline ';
} "Id =" codehighlighter0000348_495_closed_image "style =" display: none; "> {
Sum = 0 ;
Foreach ( Int V In Source)
{
This. style. Display = 'none'; codehighlighter=433_481_open_text.style.display = 'none'; codehighlighter=433_481_closed_image.style.display = 'inline'; inline = 'inline ';
} "Id =" codehighlighter%433_481_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter=433_481_open_image.style.display = 'inline'; codehighlighter=433_481_open_text.style.display = 'inline ';
} "Id =" codehighlighter1_433_481_closed_image "style =" display: none; "> {
Sum+ =V;
}
}
Long Cost = Environment. tickcount - Start;
Console. writeline ( " Msil result = {0}; cost = {1} " , Sum, cost );
Start = Environment. tickcount;
For ( Int Time = 0 ; Time < 1000 ; Time ++ )
{
This. style. Display = 'none'; codehighlighter%730_1028_open_text.style.display = 'none'; codehighlighter%730_1028_closed_image.style.display = 'inline'; response = 'inline ';
} "Id =" codehighlighter%730_1028_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter%730_1028_open_image.style.display = 'inline'; codehighlighter%730_1028_open_text.style.display = 'inline ';
} "Id =" codehighlighter%730_1028_closed_image "style =" display: none; "> {
Sum = 0 ;
Fixed ( Int * Pstart = Source, pend = & Source [source. Length - 1 ])
{
This. style. Display = 'none'; codehighlighter1_850_1014_open_text.style.display = 'none'; codehighlighter1_850_1014_closed_image.style.display = 'inline'; inline = 'inline ';
} "Id =" codehighlighter1_850_1014_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter1_850_1014_open_image.style.display = 'inline'; codehighlighter1_850_1014_open_text.style.display = 'inline ';
} "Id =" codehighlighter1_850_1014_closed_image "style =" display: none; "> {
For ( Int * Pnow = Pstart; pnow < Pend; pnow ++ )
{
This. style. Display = 'none'; codehighlighter%936_996_open_text.style.display = 'none'; codehighlighter%936_996_closed_image.style.display = 'inline'; response = 'inline ';
} "Id =" codehighlighter410936_996_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter%936_996_open_image.style.display = 'inline'; codehighlighter%936_996_open_text.style.display = 'inline ';
} "Id =" codehighlighter0000936_996_closed_image "style =" display: none; "> {
Sum+ = *Pnow;
}
}
}
Cost = Environment. tickcount - Start;
Console. writeline ( " CSP result = {0}; cost = {1} " , Sum, cost );
Fixed ( Int * Point = Source)
{
This. style. Display = 'none'; codehighlighter20171202_1539_open_text.style.display = 'none'; codehighlighter20171202_1539_closed_image.style.display = 'inline'; inline = 'inline ';
} "Id =" codehighlighter20171202_1539_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter=1202_1539_open_image.style.display = 'inline'; codehighlighter=1202_1539_open_text.style.display = 'inline ';
} "Id =" codehighlighter20171202_1539_closed_image "style =" display: none; "> {
Start = Environment. tickcount;
For ( Int Time = 0 ; Time < 1000 ; Time ++ )
{
This. style. Display = 'none'; codehighlighter=1323_1399_open_text.style.display = 'none'; codehighlighter=1323_1399_closed_image.style.display = 'inline'; inline = 'inline ';
} "Id =" codehighlighter1_1323_1399_open_image "> {
This. style. Display = 'none'; usage = 'none'; codehighlighter=1323_1399_open_image.style.display = 'inline'; codehighlighter=1323_1399_open_text.style.display = 'inline ';
} "Id =" codehighlighter1_1323_1399_closed_image "style =" display: none; "> {
Sum=Calarray (point, source. Length );
}
Cost = Environment. tickcount - Start;
Console. writeline ( " MASM result = {0}; cost = {1} " , Sum, cost );
}
In the result, the average sum of C # traversal is 630 milliseconds, the sum of C # pointer is 420 milliseconds, and the sum of assembly is 180 milliseconds. MMX is useless.