Looks like we have O(N^2*K) complexity (N=number of patents, K=number of characteristics) here... not good...
If you really have to count difference for every pair of patents, try using Bit::Vector for every line (i.e. one characteristic = one bit), calculate the number of different positions they have with XOR operation and count set bits in the resulting vector. This should take less memory and should be also faster. (?)
Although I have some doubts about the time needed to do so (you need to write to the output about (8*10^6)^2)/2 = 32 "teralines").