Home > InDesign Scripts > QuickTip: Calculate Script Execution Time

QuickTip: Calculate Script Execution Time

Building some complex script, have multiple variations of same part of the script, and want to know which one is faster? This little function is perfect for you! Take a look! 😀

Using getTime() method from Date Object we get number of milliseconds since midnight of January 1, 1970. So, what we have to do, is to set one variable on start of the script, and at the end of the script we just simply calculate difference between. I found this snippet a while ago, so it’s not my script. Also I changed it a little bit 🙂 just added support to use it multiple times.

Function

var timeDiff = {
    setStartTime:function (){d = new Date(); time  = d.getTime();},
    getDiff:function (){d = new Date(); t = d.getTime() - time; time = d.getTime(); return t;}
};

Usage (Once)

// start timer
timeDiff.setStartTime();

// do some stuff
for(var i = 0; i < 10000000; i++){}

// get result
alert("Script execution time: " + timeDiff.getDiff() / 1000 + " seconds", "IndiSnip /// Scipt execution time");

(Multiple times)

// start timer
timeDiff.setStartTime();

// do some stuff [part 1]
for(var i = 0; i < 10000000; i++){}

// get result 1
var part1 = timeDiff.getDiff();

// do some stuff [part 2]
for(var i = 0; i < 10000000; i++){}

// get result 2
var part2 = timeDiff.getDiff();

// get result
alert(part1 + " /// " + part2, "IndiSnip /// Scipt execution time");

Time we’ve got is in milliseconds, so to get seconds, just divide number with 1000 🙂

That’s it!

Have fun! 😀

Categories: InDesign Scripts Tags:
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment