Skip to content

Commit 1b34d8e

Browse files
authored
Merge branch 'pyscripter:master' into master
2 parents a96148d + e57e2fe commit 1b34d8e

File tree

7 files changed

+436
-41
lines changed

7 files changed

+436
-41
lines changed

Demos/FPC/Demo35/project1.lpi

Lines changed: 71 additions & 0 deletions
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

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
{$IFDEF MSWINDOWS}
41+
TIntArray = array[0..N - 1] of Integer;
42+
{$ELSE}
43+
TIntArray = array[0..N - 1] of NativeInt;
44+
{$ENDIF}
45+
46+
{ TBufferProtocol }
47+
48+
TBufferProtocol = class(TCustomApplication)
49+
protected
50+
procedure DoRun; override;
51+
public
52+
constructor Create(TheOwner: TComponent); override;
53+
destructor Destroy; override;
54+
end;
55+
56+
{ TBufferProtocol }
57+
58+
procedure TBufferProtocol.DoRun;
59+
var
60+
SW: TStopwatch;
61+
Sum: Int64;
62+
np: Variant;
63+
arr: Variant;
64+
np_arr: PPyObject;
65+
PyBuffer: Py_buffer;
66+
V: Variant;
67+
I: Integer;
68+
Count: Integer;
69+
ArrItem: Variant;
70+
begin
71+
try
72+
CreatePyEngine;
73+
try
74+
// Import numpy and create an array
75+
np := Import('numpy');
76+
arr := np.&array(BuiltinModule.range(N));
77+
78+
// This is the slow way to iterate the array
79+
WriteLn('Lazy but slow:');
80+
SW := TStopwatch.StartNew;
81+
Sum := 0;
82+
Count := VarPythonToVariant(arr.Length);
83+
for I := 0 to Count - 1 do
84+
begin
85+
ArrItem := BuiltinModule.int(arr.GetItem(I));
86+
Sum := Sum + Int64(VarPythonToVariant(ArrItem));
87+
end;
88+
89+
//for V in VarPyIterate(arr) do
90+
// Sum := Sum + Int64(VarPythonToVariant(BuiltinModule.int(V)));
91+
SW.Stop;
92+
WriteLn(Format('Sum from 0 to %d = %d', [N, Sum]));
93+
WriteLn('Elapsed ms: ' + SW.ElapsedMilliseconds.ToString);
94+
WriteLn;
95+
96+
WriteLn('Using Py_Buffer:');
97+
SW := TStopwatch.StartNew;
98+
np_arr := ExtractPythonObjectFrom(arr);
99+
PyEngine.PyObject_GetBuffer(np_arr, @PyBuffer, PyBUF_CONTIG);
100+
PyEngine.CheckError;
101+
try
102+
Sum := 0;
103+
for I := 0 to N - 1 do
104+
Sum := Sum + PIntArray(PyBuffer.buf)^[I];
105+
SW.Stop;
106+
WriteLn(Format('Sum from 0 to %d = %d', [N, Sum]));
107+
finally
108+
PyEngine.PyBuffer_Release(@PyBuffer);
109+
end;
110+
WriteLn('Elapsed ms: ' + SW.ElapsedMilliseconds.ToString);
111+
WriteLn;
112+
113+
// test write access
114+
PIntArray(PyBuffer.buf)^[0] := 999;
115+
if VarPythonToVariant(BuiltinModule.int(arr.GetItem(0))) = 999 then
116+
WriteLn('Successfully modified the numpy array using Py_buffer');
117+
finally
118+
DestroyEngine;
119+
end;
120+
except
121+
on E: Exception do
122+
Writeln(E.ClassName, ': ', E.Message);
123+
end;
124+
Readln;
125+
// stop program loop
126+
Terminate;
127+
end;
128+
129+
constructor TBufferProtocol.Create(TheOwner: TComponent);
130+
begin
131+
inherited Create(TheOwner);
132+
StopOnException:=True;
133+
end;
134+
135+
destructor TBufferProtocol.Destroy;
136+
begin
137+
inherited Destroy;
138+
end;
139+
140+
var
141+
Application: TBufferProtocol;
142+
begin
143+
Application:=TBufferProtocol.Create(nil);
144+
Application.Title:='BufferProtocol';
145+
Application.Run;
146+
Application.Free;
147+
end.
148+

0 commit comments

Comments
 (0)