For a more general “measure labeller” that works at any sample rate, unlimited number of measures (bars) and user defined number of beats per bar, you may be interested in the “Beat Per Minute labels” plug-in: https://forum.audacityteam.org/t/beat-per-minute-labels/20785/1
You’ll notice that it is very much faster than “M3” so looking at the code of “Beat Per Minute labels” may provide some ideas for how to make “M3” more efficient.