First you need to determine where in the code, the slow down is.
I copied the agent, and sprinkled in a few
echo "A $SECONDS"
at the end of each block of code to see which block causes the biggest increase in time. (Change the letter, so each invocation has it’s own label)