MACRO: MultiTimer 1.3

A few years back, I wrote a simple macro timing tool to help optimize some routines I was writing. This worked great, and I'm still using it today. At least, I was until I realized I wanted a better timer…

The main limitation on the previous timer was that you had to stop and start it each time you wanted to use it. I wanted a timer I could start at the beginning of the macro, and then have it record task times for various sections of the macro, and generate a report at the end, something like this:

It needed to be simple to use, and have minimal overhead (as shown in the "unallocated time" line)… and yes, that's a screenshot of my new MultiTimer, which meets both those criteria. It works very similarly to my first timer, in that it uses passed parameters in the Execute Macro routine to decide what to do.

────────────────────────────────
__ MultiTimer 1.3 Macros.kmmacros (564 KB)
────────────────────────────────

Nov 11: Version 1.3: Adds the ability to set the desired precision in the outputβ€”see the example below for how to do that. I couldn't figure out how to do this, but Peter helped me out with the complex substitutions required to pull it offβ€”thanks!

Previous versions

Nov 9: Version 1.2: This is basically a total rewrite, as my misunderstanding of the Calculation action's With Format option caused the macro to break for those who use commas as decimal separators. Basically, if you use the With Format option, you can't use that variable in a future calculation, as it will fail if it's got a comma in it. The macro now uses display variables and calculation variables to work around that issue.

There are also new start modes, including startquiet-window to have the final total time taken message appear in a window, and startquiet-silence to not see that time taken message at all (you'll still see the report).

Finally, if you preface a task name with a + sign, i.e. +Added up all costs, then a blank line will be inserted in the report above that task name (the plus sign won't show in the report).

Oct 20: Version 1.1: This contains one very nice improvement: If you call the timer with start### or startquiet###, the timer will be disabled.

Why did I add this feature? Because after using my new timer for a few days, I realized I didn't need to see the timers every time. But with a dozen or more calls to the timer scattered in my code for various tasks, it was a real pain to disable every single call to the timer. Now I can disable the timer by simply changing the start value in one call, and enable it again just as easily by removing the pound signs.

Oct 19: Version 1.0: Initial release

────────────────────────────────

HOW IT WORKS

It first checks for the 'start' keyword variations to start the timer. Add three #s anywhere in the first timer call, and you'll disable the timers. (See above for why that's useful.)

Any time it's called with a parameter that doesn't match one of the 'start...' or 'end' keywords, MultiTimer records the time since the last such call (or since timer start). And it uses the parameter as the description of the event it tracked. Add a plus sign to the task name to insert a blank row above it in the output.

USAGE

This is all much harder to describe than it is to use; here's how it looks in the included demo macro that created the above report:

Screenshot of demo macro

With MultiTimer, I now get a report on how long each section of the macro took to execute, and by using passed parameters as the description for those events, pasting one macro call is all it takes to get a new task added to the report.

As much as I liked my first macro timer, this one is a much better tool to help me understand where to focus my time on performance improvements in complex macros.

SETTING PRECISION

Version 1.3 includes the ability to set the precision of the resultsβ€”0.00, 0.00, etc. This requires changing two things in the macro: The precision itself, and the required padding. The precision is set at the top of the macro:

You also need to set (or at least check) the amount of padding, and that's much further down in the macro, in the section that handles the end parameter:

The comment text explains why this value is important, but basically, you need to set enough padding to allow the macro to align all your numbers at their stated precision, based on the length of the largest value.

It's tricky to explain, but easy to see in action with the test macro, so play around with the random times (in the test macro) and padding values (in the macro itself) to see how it works.

-rob.

6 Likes

Version 1.1 is out, and it now lets you easily disable all timer calls just by including three pound signs (###) anywhere in the start or startquiet calls.

With a dozen or more calls to my timer in a macro I'm working on, it was a real pain to disable all of them while I was working on things. Now I can leave all those calls enabled, and just change the start value. When I want the timers back, I just remove the ### and it's good to go.

-rob.

1 Like

Version 1.2 is out, and it's a very important update: It now runs properly for both "0,000.00" and "0.000,00" number formats. The old one broke due to my misunderstanding of how the "With Format" option affected numbers used in Calculation actions.

It also adds a couple new 'start' modes, and you can insert blank lines in the report by prefacing a task name with a plus sign, i.e. +Calculated total costs. The plus sign won't be printed in the report.

I also cleaned up the code quite a bit, and set all global variables to start with rg_MT_, making them easier to find and delete if something goes wrongβ€”the macro will delete them itself if it finishes successfully, but if it doesn't, just delete any globals that start with rg_MT_, and you'll be good to go.

-rob.

1 Like

Hey, Rob. Should the sample macro still be included and I'm missing it? Trying to test this bad boy out! Thanks for always sharing cool stuff :sunglasses:

Whoops, yea it should. Included nowβ€”and thank you for the bit of AppleScript in your UUID dictionary post that let me make my MacroBackerUpper oh so much better! It was only a bit, but it got me going on getting all the data I needed out.

-rob.

1 Like

Right on! I worked hard on that one and it still runs everyday at 9am like a champ. So glad to hear you got something from it. Never know!

MultiTimer 1.3 is out, and it adds the ability to set the desired precision of the displayed results. This requires some really wild variable substitution that I had no idea how to pull off, but Peter showed me the secret sauce:

So with that, you can now set the desired precision, which also requires you to think about paddingβ€”read about setting both in the first post above.

-rob.

Those nested %%% can be daunting! :scream:

1 Like

Just found a use for this, easy to use, love it :pray:t2: