L
luzhouman
Unregistered / Unconfirmed
GUEST, unregistred user!
我也来一个ASM版:
在我的电脑上100000需要0.6秒左右, P4 2.6, 单512M
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
APrime: string = '';
MaxValue: DWORD = 100000;
procedure WritePrime(iPrime: Integer);
begin
APrime:=APrime+IntToStr(iPrime)+#13#10;
end;
function GetPrime(): Boolean;
asm
PUSH EBX
PUSH EDI
mov eax, 2
call WritePrime
mov eax, 3
call WritePrime
mov edi, 5
@@While_MaxValue:
mov eax, edi
cmp eax, [MaxValue]
jnbe @@End_While_MaxValue
mov ebx, 3
@@While_Factor:
mov eax, ebx
mul eax
jo @@End_While_Factor
cmp eax, edi
jnb @@End_While_Factor
mov eax, edi
mov edx, 0
div ebx
cmp edx, 0
je @@End_While_Factor
add ebx, 2
jmp @@While_Factor
@@End_While_Factor:
je @@End_If
mov eax, edi
call WritePrime
@@End_If:
mov eax, edi
add eax, 2
mov edi, eax
jmp @@While_MaxValue
@@End_While_MaxValue:
mov eax, 1
POP EDI
POP EBX
end;
procedure TForm1.Button1Click(Sender: TObject);
var
lInt, lsTime, leTime: Int64;
begin
try
APrime:='';
QueryPerformanceFrequency(lInt);
QueryPerformanceCounter(lsTime);
if GetPrime() then ListBox1.Items.Text:=APrime;
QueryPerformanceCounter(leTime);
ShowMessage(Format('完成时间(秒):%.10f', [(leTime-lsTime) / lInt]));
except
ShowMessage('计算错误!!!');
end;
end;
end.
在我的电脑上100000需要0.6秒左右, P4 2.6, 单512M
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
var
APrime: string = '';
MaxValue: DWORD = 100000;
procedure WritePrime(iPrime: Integer);
begin
APrime:=APrime+IntToStr(iPrime)+#13#10;
end;
function GetPrime(): Boolean;
asm
PUSH EBX
PUSH EDI
mov eax, 2
call WritePrime
mov eax, 3
call WritePrime
mov edi, 5
@@While_MaxValue:
mov eax, edi
cmp eax, [MaxValue]
jnbe @@End_While_MaxValue
mov ebx, 3
@@While_Factor:
mov eax, ebx
mul eax
jo @@End_While_Factor
cmp eax, edi
jnb @@End_While_Factor
mov eax, edi
mov edx, 0
div ebx
cmp edx, 0
je @@End_While_Factor
add ebx, 2
jmp @@While_Factor
@@End_While_Factor:
je @@End_If
mov eax, edi
call WritePrime
@@End_If:
mov eax, edi
add eax, 2
mov edi, eax
jmp @@While_MaxValue
@@End_While_MaxValue:
mov eax, 1
POP EDI
POP EBX
end;
procedure TForm1.Button1Click(Sender: TObject);
var
lInt, lsTime, leTime: Int64;
begin
try
APrime:='';
QueryPerformanceFrequency(lInt);
QueryPerformanceCounter(lsTime);
if GetPrime() then ListBox1.Items.Text:=APrime;
QueryPerformanceCounter(leTime);
ShowMessage(Format('完成时间(秒):%.10f', [(leTime-lsTime) / lInt]));
except
ShowMessage('计算错误!!!');
end;
end;
end.