in reply to Some code optimization

Have you tried a profiler? I have good experience with Devel::NYTProf.