in reply to Optimization problem?

The problem is line buffering of STDOUT, and can be fixed with $| = 1;.

See also Perl only prints complete lines? which is exactly the same problem.