|
| 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