0

I'm comparing invoices numbers and amounts with two excel files in python code it can be finished very fast but when I used C# it was very slow

python code :

for i in range(2,ws1.max_row) :
    for n in range(2,ws2.max_row):
        if str(ws1['B' + str(i)].value) == str(ws2['B' + str(n)].value) and ws1['C' + str(i)].value == ws2['C' + str(n)].value:
            ws1['B' + str(i)].fill = redFill
            ws1['C' + str(i)].fill = redFill
            ws2['B' + str(n)].fill = redFill
            ws2['C' + str(n)].fill = redFill

        elif str(ws1['B' + str(i)].value) == str(ws2['B' + str(n)].value) and ws1['C' + str(i)].value != ws2['C' + str(n)].value:
            ws1['B' + str(i)].fill = yellowFill
            ws2['B' + str(n)].fill = yellowFill
            ws1['C' + str(i)].fill = yellowFill
            ws2['C' + str(n)].fill = yellowFill
            ws1['D' + str(i)].value = ws2['C' + str(n)].value - ws1['C' + str(i)].value
            ws2['D' + str(n)].value = ws1['C' + str(i)].value - ws2['C' + str(n)].value

C# code :

for (int i = 2; i < ws5.UsedRange.Rows.Count; i++)
{
    for (int n = 2; n < ws6.UsedRange.Rows.Count; n++)
    {
        if (Convert.ToDouble(ws5.Cells[i, 3].Value) == Convert.ToDouble(ws6.Cells[n, 3].Value) && Convert.ToString(ws5.Cells[i, 2].Value) == Convert.ToString(ws6.Cells[n, 2].Value))
        {
            ws5.Cells[i, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);
            ws5.Cells[i, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);
            ws6.Cells[n, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);
            ws6.Cells[n, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Orange);
        }
        else if (Convert.ToDouble(ws5.Cells[i, 3].Value) != Convert.ToDouble(ws6.Cells[n, 3].Value) && Convert.ToString(ws5.Cells[i, 2].Value) == Convert.ToString(ws6.Cells[n, 2].Value))

        {
            ws5.Cells[i, 4].Value = Convert.ToDouble(ws6.Cells[n, 3].Value) - Convert.ToDouble(ws5.Cells[i, 3].Value);
            ws6.Cells[n, 4].Value = Convert.ToDouble(ws5.Cells[i, 3].Value) - Convert.ToDouble(ws6.Cells[n, 3].Value);

            ws5.Cells[i, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
            ws5.Cells[i, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
            ws6.Cells[n, 2].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);
            ws6.Cells[n, 3].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow);

        }
    }
}
Najm
  • 1
  • surround your differents function calls by time trackers to get the execution time https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution then you can dive into why the function you use is slower. Maybe your function in python has been optimized in Cpython and thus is faster than its C# counterpart – Maxime Nov 05 '22 at 13:43

0 Answers0