-
-
Notifications
You must be signed in to change notification settings - Fork 317
/
Copy pathMainForm.pas
152 lines (135 loc) · 4.23 KB
/
MainForm.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
unit MainForm;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, SynEdit, Vcl.StdCtrls,
PythonEngine, PythonGUIInputOutput, SynEditPythonBehaviour,
SynEditHighlighter, SynEditCodeFolding, SynHighlighterPython, Vcl.ExtCtrls;
type
TForm1 = class(TForm)
sePythonCode: TSynEdit;
HeaderControl1: THeaderControl;
Panel1: TPanel;
Splitter1: TSplitter;
Panel2: TPanel;
HeaderControl2: THeaderControl;
mePythonOutput: TMemo;
SynPythonSyn: TSynPythonSyn;
SynEditPythonBehaviour: TSynEditPythonBehaviour;
PythonEngine: TPythonEngine;
PythonGUIInputOutput: TPythonGUIInputOutput;
btnRun: TButton;
PythonModule: TPythonModule;
Button1: TButton;
Button2: TButton;
procedure btnRunClick(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure PythonModuleEvents0Execute(Sender: TObject; PSelf, Args: PPyObject;
var Result: PPyObject);
procedure PythonModuleEvents1Execute(Sender: TObject; PSelf, Args: PPyObject;
var Result: PPyObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
uses
System.Threading,
System.Math;
procedure TForm1.btnRunClick(Sender: TObject);
begin
GetPythonEngine.ExecString(UTF8Encode(sePythonCode.Text));
end;
function IsPrime(x: Integer): Boolean;
begin
if (x <= 1) then Exit(False);
var q := Floor(Sqrt(x));
for var i := 2 to q do
if (x mod i = 0) then
Exit(False);
Exit(True);
end;
function CountPrimes(MaxN: integer): integer;
begin
var Count := 0;
TParallel.&For(2, MaxN, procedure(i: integer)
begin
if IsPrime(i) then
AtomicIncrement(Count);
end);
Result := Count;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sePythonCode.Text :=
'from delphi_module import delphi_is_prime'#13#10 + //0
'from timeit import Timer'#13#10 + //1
''#13#10 + //3
'def count_primes(max_n):'#13#10 + //4
' res = 0'#13#10 + //5
' for i in range(2, max_n + 1):'#13#10 + //6
' if delphi_is_prime(i):'#13#10 + //7
' res += 1'#13#10 + //8
' return res'#13#10 + //9
''#13#10 + //10
'def test():'#13#10 + //11
' max_n = 1000000'#13#10 + //12
' print(f''Number of primes between 0 and {max_n} = {count_primes(max_n)}'')'#13#10 + //13
''#13#10 + //14
'def main():'#13#10 + //15
' print(f''Elapsed time: {Timer(stmt=test).timeit(1)} secs'')'#13#10 + //16
''#13#10 + //17
'if __name__ == ''__main__'':'#13#10 + //18
' main()'#13#10; //19
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
sePythonCode.Text :=
'from delphi_module import delphi_count_primes'#13#10 + //0
'from timeit import Timer'#13#10 + //1
''#13#10 + //10
'def test():'#13#10 + //11
' max_n = 1000000'#13#10 + //12
' print(f''Number of primes between 0 and {max_n} = {delphi_count_primes(max_n)}'')'#13#10 + //13
''#13#10 + //14
'def main():'#13#10 + //15
' print(f''Elapsed time: {Timer(stmt=test).timeit(1)} secs'')'#13#10 + //16
''#13#10 + //17
'if __name__ == ''__main__'':'#13#10 + //18
' main()'#13#10; //19
end;
procedure TForm1.PythonModuleEvents0Execute(Sender: TObject; PSelf, Args:
PPyObject; var Result: PPyObject);
Var
N: Integer;
begin
with GetPythonEngine do
if PyArg_ParseTuple(Args, 'i:delphi_is_prime',@N) <> 0 then
begin
if IsPrime(N) then
Result := PPyObject(Py_True)
else
Result := PPyObject(Py_False);
Py_INCREF(Result);
end else
Result := nil;
end;
procedure TForm1.PythonModuleEvents1Execute(Sender: TObject; PSelf, Args:
PPyObject; var Result: PPyObject);
Var
N: Integer;
begin
with GetPythonEngine do
if PyArg_ParseTuple(Args, 'i:delphi_is_prime',@N) <> 0 then
begin
Result := PyLong_FromLong(CountPrimes(N));
Py_INCREF(Result);
end else
Result := nil;
end;
end.