Two recommendations: first, carefully read and apply the performance tips, particularly the admonition to avoid non-constant global variables. Second, please share your code as text wrapped in triple backticks so other users can easily read and copy-paste it (see here).