[Adv. Macro Trick:] Calculations Progress Bar in Matptool

Discuss macro implementations, ask for macro help (to share your creations, see User Creations, probably either Campaign Frameworks or Drop-in Resources).

Moderators: dorpond, trevor, Azhrei, giliath, jay, Mr.Ice

Post Reply
User avatar
wolph42
Winter Wolph
Posts: 9999
Joined: Fri Mar 20, 2009 5:40 am
Location: Netherlands
Contact:

[Adv. Macro Trick:] Calculations Progress Bar in Matptool

Post by wolph42 »

Here's a nice trick if you would like to have a progress indicator while running a loop in Maptool:
progress bar code

Code: Select all

[h:abort(1)]

[h, if(json.type(macro.args) != "UNKNOWN"), CODE:{
    [totalIteration = arg(0)]
    [currentIteration = arg(1)]
};{ 
    [totalIteration = 2000]
    [currentIteration = 1]
}]

[frame("Progress Bar", "width=410; height=10; temporary=0"): {
    <table width=305px bgcolor='red'><tr><td>
        [h:prog.percentage = floor(100*currentIteration/totalIteration)]
        <table width=[r:3*prog.percentage]px bgcolor = 'green' color='white'><tr><td>[r:prog.percentage]% [r:if(band(currentIteration,1), "0", "o")]</td></tr>
    </td></tr></table>
}]

[h:'<!----------------------------- here you can do your calculations -->']

[h: setLibProperty("iteration"+currentIteration, currentIteration, "lib:Test")]


[h:'<!----------------------------- /here you can do your calculations -->']

[h:currentIteration = currentIteration + 1]
[h: link = macroLinkText("progress bar@Lib:Test", "self", json.append("",totalIteration, currentIteration))]
[h, if(currentIteration < totalIteration):execLink(link,1); closeFrame("Progress Bar")]
 
This trick has the obvious big disadvantage that it slow down your code... however this can be partially remedied by putting a part of the loop inside this deferred routine. E.g. you can introduce 

Code: Select all

[for (i, currentIteration*10, currentIteration*10 +10), CODE:{ <!--do your stuff here-->}]]  
so the deferred loop is called not every step but every 10 steps. (or 100 or 1000).

And there are a couple of big advantages:
1. you can see the progress (duh), especially for long interations this can be very usefull as i sometimes have iterations that can take upto 15 minutes and you have no clue whether its still running or not...
2. loops > 1000 yes, cause its deferred no more 'max loop limit' issues
3. you can still interact with MT, albeit very slow, because its a deferred loop you can still interact.
4. you can break it off at any moment: because of 3. you can just edit the running macro and set the 'abort(1)' to 'abort(0)' and the loop immediately stops.

edit: if case your curious on a win7, Quad CPU @ 2.66Hz (64 bits) I run the progress bar without anything 'here you can do your calculations' (so a base benchmark). I've done 100, 1000 and 10000 loops and it very consistently takes 14 ms per cycle. So in case of 10k loops the progress bar will add an additional 2m20s to the run time... unless I choose to use update the bar every 100 cycles in which case it will add only 1,5 second to the routine!

Post Reply

Return to “Macros”