If following up on point_1 doesn't clarify your problem, you may wish to quantify "so much time" for us. I suspect -- from a cursory reading of your code, that the expectations that led you to complain about "so much time" may have been excessively optomistic: looping thru two 2GB+ files simultaneously is unlikely to be fast with a read per line per file to execute and a regex that relies so heavily on wildcards.