Most of your questions are answered in the following video:
To summarize, the only thing you can do to improve the time it takes for scans to process is to apply top level filters that reduce the number of ticker symbols to only those that you would actually trade. In most cases, users should use the highest possible time frame that still encompasses the specific signals you are scanning for. Lower time frames require more processing power.
In addition to this, you will want to view the following link to understand how much historical data is included at each time frame you select for your Study Filters:
Scroll down to the very bottom and read the section titled: "Data Limitations"
By carefully adjusting each of these parameters you can arrive at a scan that runs as efficiently as possible. There is really nothing you can do in the code of your Study Filter to make things run faster. But there are certain functions that should be avoided in the code for any scan. Those functions that should be avoided are:
HighestAll(), LowestAll(), TotalSum() and you should avoid using the looping statement in Thihnkorswim which is known as "fold":
That's it. Nothing more you can do.