http://www.perlmonks.org?node_id=526497


in reply to Efficient code.

Alternations in a pattern involve backtracking. I can’t imagine that a pattern with 50,000 400 of them executed 50,000 times will perform like anything other than molasses. The matching engine doesn’t work by magic.

If you really only need to compare line 1 in A to line 1 in B, line 2 in A to line 2 in B, etc, then doing it line-by-line will be much faster. 50,000 lines is nothing; computers eat such files for breakfast nowadays. If there isn’t a need to run the comparison extremely frequently, then the effort of optimising it will be wasted.

Update: I misread the problem; mea culpa. I suggest using a hash, as holli proposed – they’re designed for exactly the sort of problem your friend has to solve.

Of course the above is just conjecture. If I really cared, I’d write some code to benchmark and possibly profile, because a programmer is always wrong about how fast code is and what it spends the most time doing.

But never mind the performance question: the next guy who needs to touch the code will actually be able to understand what it’s doing if your friend writes it the straightforward way, and unglamorous as it may be to write simple, straightforward code, it is 100× more important than performance. “Correct code is easier to optimise than optimised code is to correct” and all that. Tell your friend to stop wasting significant programmer time on saving irrelevant amounts of computer time before he even knows there’s a problem.

Makeshifts last the longest.