Skip to content

Commit b2f9224

Browse files
committed
Added FPC Demo35
1 parent fc96036 commit b2f9224

File tree

3 files changed

+406
-0
lines changed

3 files changed

+406
-0
lines changed

Demos/FPC/Demo35/project1.lpi

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<CONFIG>
3+
<ProjectOptions>
4+
<Version Value="12"/>
5+
<PathDelim Value="\"/>
6+
<General>
7+
<Flags>
8+
<MainUnitHasCreateFormStatements Value="False"/>
9+
<MainUnitHasScaledStatement Value="False"/>
10+
</Flags>
11+
<SessionStorage Value="InProjectDir"/>
12+
<Title Value="BufferProtocol"/>
13+
<UseAppBundle Value="False"/>
14+
<ResourceType Value="res"/>
15+
</General>
16+
<BuildModes>
17+
<Item Name="Default" Default="True"/>
18+
</BuildModes>
19+
<PublishOptions>
20+
<Version Value="2"/>
21+
<UseFileFilters Value="True"/>
22+
</PublishOptions>
23+
<RunParams>
24+
<FormatVersion Value="2"/>
25+
</RunParams>
26+
<RequiredPackages>
27+
<Item>
28+
<PackageName Value="P4DLaz"/>
29+
</Item>
30+
</RequiredPackages>
31+
<Units>
32+
<Unit>
33+
<Filename Value="project1.lpr"/>
34+
<IsPartOfProject Value="True"/>
35+
</Unit>
36+
<Unit>
37+
<Filename Value="stopwatch.pas"/>
38+
<IsPartOfProject Value="True"/>
39+
</Unit>
40+
</Units>
41+
</ProjectOptions>
42+
<CompilerOptions>
43+
<Version Value="11"/>
44+
<PathDelim Value="\"/>
45+
<Target>
46+
<Filename Value="project1"/>
47+
</Target>
48+
<SearchPaths>
49+
<IncludeFiles Value="$(ProjOutDir)"/>
50+
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
51+
</SearchPaths>
52+
<Linking>
53+
<Debugging>
54+
<DebugInfoType Value="dsDwarf3"/>
55+
</Debugging>
56+
</Linking>
57+
</CompilerOptions>
58+
<Debugging>
59+
<Exceptions>
60+
<Item>
61+
<Name Value="EAbort"/>
62+
</Item>
63+
<Item>
64+
<Name Value="ECodetoolError"/>
65+
</Item>
66+
<Item>
67+
<Name Value="EFOpenError"/>
68+
</Item>
69+
</Exceptions>
70+
</Debugging>
71+
</CONFIG>

Demos/FPC/Demo35/project1.lpr

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
program project1;
2+
3+
{$mode delphi}{$H+}
4+
5+
uses
6+
{$IFDEF UNIX}
7+
cthreads,
8+
{$ENDIF}
9+
Classes, SysUtils, CustApp,
10+
{ you can add units after this }
11+
Variants,
12+
PythonEngine,
13+
VarPyth,
14+
stopwatch;
15+
16+
var
17+
PyEngine: TPythonEngine;
18+
19+
procedure CreatePyEngine;
20+
begin
21+
MaskFPUExceptions(True, True);
22+
PyEngine := TPythonEngine.Create(nil);
23+
PyEngine.Name := 'PythonEngine';
24+
PyEngine.UseLastKnownVersion := False;
25+
PyEngine.RegVersion:= '3.12';
26+
PyEngine.DllName:= 'python312.dll';
27+
PyEngine.LoadDll;
28+
end;
29+
30+
procedure DestroyEngine;
31+
begin
32+
PyEngine.Free;
33+
end;
34+
35+
const
36+
N = 100000;
37+
38+
type
39+
PIntArray = ^TIntArray;
40+
TIntArray = array[0..N - 1] of Integer;
41+
42+
{ TBufferProtocol }
43+
44+
TBufferProtocol = class(TCustomApplication)
45+
protected
46+
procedure DoRun; override;
47+
public
48+
constructor Create(TheOwner: TComponent); override;
49+
destructor Destroy; override;
50+
end;
51+
52+
{ TBufferProtocol }
53+
54+
procedure TBufferProtocol.DoRun;
55+
var
56+
SW: TStopwatch;
57+
Sum: Int64;
58+
np: Variant;
59+
arr: Variant;
60+
np_arr: PPyObject;
61+
PyBuffer: Py_buffer;
62+
V: Variant;
63+
I: Integer;
64+
Count: Integer;
65+
ArrItem: Variant;
66+
begin
67+
try
68+
CreatePyEngine;
69+
try
70+
// Import numpy and create an array
71+
np := Import('numpy');
72+
arr := np.&array(BuiltinModule.range(N));
73+
74+
// This is the slow way to iterate the array
75+
WriteLn('Lazy but slow:');
76+
SW := TStopwatch.StartNew;
77+
Sum := 0;
78+
Count := VarPythonToVariant(arr.Length);
79+
for I := 0 to Count - 1 do
80+
begin
81+
ArrItem := BuiltinModule.int(arr.GetItem(I));
82+
Sum := Sum + Int64(VarPythonToVariant(ArrItem));
83+
end;
84+
85+
//for V in VarPyIterate(arr) do
86+
// Sum := Sum + Int64(VarPythonToVariant(BuiltinModule.int(V)));
87+
SW.Stop;
88+
WriteLn(Format('Sum from 0 to %d = %d', [N, Sum]));
89+
WriteLn('Elapsed ms: ' + SW.ElapsedMilliseconds.ToString);
90+
WriteLn;
91+
92+
WriteLn('Using Py_Buffer:');
93+
SW := TStopwatch.StartNew;
94+
np_arr := ExtractPythonObjectFrom(arr);
95+
PyEngine.PyObject_GetBuffer(np_arr, @PyBuffer, PyBUF_CONTIG);
96+
PyEngine.CheckError;
97+
try
98+
Sum := 0;
99+
for I := 0 to N - 1 do
100+
Sum := Sum + PIntArray(PyBuffer.buf)^[I];
101+
SW.Stop;
102+
WriteLn(Format('Sum from 0 to %d = %d', [N, Sum]));
103+
finally
104+
PyEngine.PyBuffer_Release(@PyBuffer);
105+
end;
106+
WriteLn('Elapsed ms: ' + SW.ElapsedMilliseconds.ToString);
107+
WriteLn;
108+
109+
// test write access
110+
PIntArray(PyBuffer.buf)^[0] := 999;
111+
if VarPythonToVariant(BuiltinModule.int(arr.GetItem(0))) = 999 then
112+
WriteLn('Successfully modified the numpy array using Py_buffer');
113+
finally
114+
DestroyEngine;
115+
end;
116+
except
117+
on E: Exception do
118+
Writeln(E.ClassName, ': ', E.Message);
119+
end;
120+
Readln;
121+
// stop program loop
122+
Terminate;
123+
end;
124+
125+
constructor TBufferProtocol.Create(TheOwner: TComponent);
126+
begin
127+
inherited Create(TheOwner);
128+
StopOnException:=True;
129+
end;
130+
131+
destructor TBufferProtocol.Destroy;
132+
begin
133+
inherited Destroy;
134+
end;
135+
136+
var
137+
Application: TBufferProtocol;
138+
begin
139+
Application:=TBufferProtocol.Create(nil);
140+
Application.Title:='BufferProtocol';
141+
Application.Run;
142+
Application.Free;
143+
end.
144+

0 commit comments

Comments
 (0)