I want to make a python code but it requires a lot of calculations. I thought it would be good to write some c#-based method and invoke the dll from python. For testing, I wrote some simple code (see below). Here is an example code in c#:
namespace TestFrameWork
{
public class Class1
{
public static float Summ(float[][] arr, int rowsHeight, int colsWidth)
{
float value = 0;
for (int col = 0; col < colsWidth; col++)
{
for (int row = 0; row < rowsHeight; row++)
{
value += 1;
}
}
return value;
}
}
}
And the test:
namespace main
{
class Program
{
static void Main(string[] args)
{
float[][] inputs = Enumerable
.Range(0, 4000)
.Select(i => new float[33000])
.ToArray();
var watch = System.Diagnostics.Stopwatch.StartNew();
var result = Class1.Summ(inputs, 4000, 33000);
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
Console.WriteLine(result);
Console.WriteLine($"Elapsed time: {elapsedMs} ms");
}
}
}
And the code for loading the method from Python:
import numpy as np
import clr
import time
w = 4000
h = 33000
image = np.ones((h,w))
clr.AddReference(r"C:\Users\nagym\source\repos\TestFrameWork\bin\Debug\TestFrameWork.dll")
from TestFrameWork import Class1
image1 = image.data.tolist()
start = time.time()
result = Class1.Summ(image1,w,h)
print(f"Elapsed time c# : {(time.time() - start)*1000} ms")
Results:
VS C# -> Elapsed time: 318 ms
Python -> Elapsed time c# : 24567.59548187256 ms
Using Visual Studio to run the C# code is much faster. But why? I am using the same DLL file.