[Python] Difflib of Python taking too much time to compare

Discussão em 'Python' iniciado por Stack, Outubro 3, 2024.

  1. Stack

    Stack Membro Participativo

    I am using difflib to compare the output for two test files, but when there is too much difference, the difflib is taking too much time, more than a minute, is there any other alternative to find the first mismatched line in both strings?

    Example:

    string 1 = "Yes\n1\nYes\n2\nNo\n3\n..."

    string 2 = "Yes\n1\nNo\n2\nYes\n3\n..."

    first mismatched line = 3

    and somehow I know, that "yes\n2" is my test case that failed, it should have been "No\n2" (I have the supporting data for that)

    For now, I am using difflib to compare the two strings and get the difference array, and then I get the first mismatch (+ or -) and get the failed test line according to it, and then it's pretty simple for me to get the failed test case.

    But the problem with it is when there is too much difference in the strings, it is taking more than a minute.

    The possible solutions that I can think of now are -

    1. Using difflib in a way that it doesn't compare all files but only gives me the first difference
    2. what if I do line by line comparison, can it impact the performance
    3. Using Linux diff command, but it doesn't provide an array like difflib( which makes it easier to get the mismatched line number).

    Continue reading...

Compartilhe esta Página