Are there are any ways to measure the time it takes for a portion of script to execute?
No, don't think I know of any.
Also remember googling for such timing options wrt factorio specifically, and iirc all feature request of that kind end up bumping into the issue that this kind of thing is guaranteed to be non-deterministic (measuring local cpu time), and as such is guaranteed to break multiplayer, so impossible under developers' vision for the game.
Factorio probably uses mostly-same open-source lua 5.2 implementation as can be downloaded on the internet, and you can easily copy your code routine there, provide same inputs and run it in a loop like 1M times, measure the total, which I bet would be very representative of its performance in factorio (to within couple %), especially for comparison with other code.
On linux I'd just run it as time lua5.2 test.lua
from shell, but you can probably wrap it into ms timers and run with less fancy shell on windows to roughly same effect.
And of course maybe you can check general mod performance profiling screen (F6 or somesuch key - check wiki), and disable other mods if they don't leave much cpu time on ticks for this one.
Are there particularly expensive types of operations to watch out for?
Maybe you can make lots of things run even less code by using "irq" signals rather than static delays, if things happen less often than 1/s.
Generally I'd watch out of any iteration, and especially nested loops, which are subject to blowing up exponentially, but that's probably obvious.
I'd maybe also look into using less combinators and more connected circuit networks, and using stock combinators more.
Never really played with very large bases myself, and for most of my games only like 5-10 of these on the map was enough, with many more regular constant/arithmetic/decider ones (like hundreds, incl. automating most train loading), but I can see how one might want to go entirely for lua combinators instead of ever designing gate logic.
Oh, and I also play on much slower speed than 1x, for a much more chill game, so effectively have 2x-4x more time for game/mod logic on each tick, maybe you can use that trick too for a smoother experience.
It's probably too obvious as well, but I think just running the game on faster CPU core(s) can allow it to do more on each frame.
Mostly played the game on AMD Phenom-2 machine I bought in 2011 myself, so suspect your issue might have an entirely different scale that no modern CPU can fix :)