[Python.NET] [Pythonnet] Python to c# long conversion time

2022-06-27 Thread Jordan GUILLOT
Hi,

I'm using Pythonnet to import proprietary python package from Windows 
Application.
When my python package return a large byte array in C# world, the type 
conversion takes lot of time.

To illustrate this long conversion time, I made a simple example:

  1.  A Python package with a method which return a byte array:
class MyClass:
 @staticmethod
 def get_bytes_from_py():
array = np.arange(0, 72, 
dtype=np.uint8)
return bytes(array)


  1.  An application console to call my Python package and which convert 
'bytes' type into 'byte[]' type

static void Main(string[] args)
{
Runtime.PythonDLL = "c:\\Python38\\python38.dll";
PythonEngine.Initialize();

Stopwatch st = new Stopwatch();
st.Start();

using (Py.GIL())
{
dynamic myPackage = Py.Import("mypackage");
dynamic myClass = myPackage.MyClass();
dynamic myPyByteArray = myClass.get_bytes_from_py();

long t0 = st.ElapsedMilliseconds;
byte[] myCsharpByteArray = myPyByteArray;
long t1 = st.ElapsedMilliseconds;

Console.WriteLine("Conversion takes {0}ms", t1-t0);
}

st.Stop();
}


  1.  The console output:

"Conversion takes 303ms"


Is this the right way for this conversion?

Environment:
Python: v3.8.10
Pythonnet:

  *   Pypi: v2.5.2
  *   Nuget : 3.0.0-preview2022-06-03
Windows 10 Pro
.NET framework 4.7.2


Regards,
Jordan GUILLOT


___
PythonNet mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: [email protected]


[Python.NET] Re: [Pythonnet] Python to c# long conversion time

2022-06-27 Thread LOST _
Please, use myPyByteArray.GetBuffer() and the PyBuffer.Read method to copy from a NumPy array. I created a feature request to make the default conversion mechanism take advantage of PyBuffer some time in the future. Regards,Victor From: Jordan GUILLOTSent: Monday, June 27, 2022 8:19 AMTo: [email protected]: [Python.NET] [Pythonnet] Python to c# long conversion time Hi, I’m using Pythonnet to import proprietary python package from Windows Application.When my python package return a large byte array in C# world, the type conversion takes lot of time. To illustrate this long conversion time, I made a simple example:A Python package with a method which return a byte array:class MyClass:     @staticmethod     def get_bytes_from_py():        array = np.arange(0, 72, dtype=np.uint8)        return bytes(array) An application console to call my Python package and which convert ‘bytes’ type into ‘byte[]’ type static void Main(string[] args)    {    Runtime.PythonDLL = "c:\\Python38\\python38.dll";    PythonEngine.Initialize();    Stopwatch st = new Stopwatch();    st.Start(); using (Py.GIL())    {    dynamic myPackage = Py.Import("mypackage");    dynamic myClass = myPackage.MyClass();    dynamic myPyByteArray = myClass.get_bytes_from_py(); long t0 = st.ElapsedMilliseconds;    byte[] myCsharpByteArray = myPyByteArray;    long t1 = st.ElapsedMilliseconds; Console.WriteLine("Conversion takes {0}ms", t1-t0);    } st.Stop();    } The console output:“Conversion takes 303ms” Is this the right way for this conversion? Environment:Python: v3.8.10Pythonnet:Pypi: v2.5.2Nuget : 3.0.0-preview2022-06-03Windows 10 Pro.NET framework 4.7.2 Regards,Jordan GUILLOT    
___
PythonNet mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/pythonnet.python.org/
Member address: [email protected]