Your speed problem is probably caused by using up all of your RAM by putting the entire files in memory. If you can figure out an algorithm that doesn't do this, you'll be much better off. Also, if you post the code you have now, it will make it easier to offer additional help.
When's the last time you used duct tape on a duct? --Larry Wall