Fastest Way To Find Mismatch Positions Between Two Strings of the Same Length

前端 未结 9 1385
后悔当初
后悔当初 2020-12-28 22:08

I have a millions of pairs of string of same length which I want to compare and find the position where it has mismatches.

For example for each $str1 a

9条回答
  •  滥情空心
    2020-12-28 22:53

    You're making 2 calls to substr for each character comparison which is probably what's slowing you down.

    A few optimizations I would make

    @source = split //,$str_source  #split first rather than substr
    @base = split //, $str_base
    
    for $i (0 .. length($str_source)) {
       $mism_pos{$1} = 1 if ($source[$i] ne $base); #hashing is faster than array push
    }
    
    return keys $mism_pos
    

提交回复
热议问题