You operate mostly in global scope with global variables. Check out the performance tips section of the manual for some pointers and explanations of why this leads to slow code. Try putting all code in functions and make sure that variables that are accessed on performance sensitive parts of your code are either local or constant.
5 Likes