I'll not even try to understand what your code does (and other already provided some material for you on that), but here are some general guidelines:
- You should run a profiler over it: see Devel::NYTProf and identify the areas that are using more time/CPU so you can start with them.
- Since you are working inside loops, check out if you can get ride of them. If not, check some algorithms like Schwartzian_transform.
- Benchmark your code before and after the changes you implement to see if they are effective. See Dumbbench.
Alceu Rodrigues de Freitas Junior
---------------------------------
"You have enemies? Good. That means you've stood up for something, sometime in your life." - Sir Winston Churchill