Profiling / Benchmarking

You will find a general example for the use of timer here.

Version :
16.10.2024, 11:40 Uhr

Profiling / Benchmarking

static int start(char* label)

Starts the timing of a cscript program section.

Name Type Default Description
Return int   0 ot ErrorCode
label String oder char* - freely selectable name of the timer

Simple timer to measure the execution time of script instructions.

int main ()
{
    //  ...
    timer::reset ();
    for (i = 0; i < 10; ++i) 
    {
        timer::start ("create and save snapshot");
// some instructions ...
timer::stop ("create and save snapshot"); } timer::dump (); return 0; }

Version 2.1 (P/R 770)

priint:comet InDesign® Plug-Ins, comet_pdf

timer::stop
system::time_diff

static int frame_start(
  ItemRef frameRef,
  char* key,
  float intervall,
  int flags = 0)

Starts a timer that executes certain layout rules of the frame at the next idle time of the program. A frame may have any number of such timers. But please pay attention to short execution times. If the frame is deleted or the document is closed, active timers are automatically removed.

In the parameter key of the function you define which layout rule(s) the timer should execute. To do this, you create a If Send event block within the layout rules for the event key. All rules within this block are then executed exclusively by this timer. Here is a screenshot for a timer called TIMER:Mein Timer:

Please note : To activate the timer condition, at least one rule of the condition must have set an execution time. It does not matter which execution time you choose, it is only important that at least one condition is activated.

Name Type Default Description
Return int   0 or ErrorCode

-1199 : Called by comet_pdf
frameRef ItemRef - The timer is to be defined for this frame. If the frame is deleted or the document is closed, the timer is automatically stopped.
key String or char* - Name of the event whose layout rules are to be executed.
intervall float - Start/interval of the timer in seconds

<= 1.0 : Running the timer once
>= 1.0 : Repeated execution of the timer at the specified interval

The minimum absolute length of the interval is 1 second!
flags int 0 Additional information for the timer

0 : no further details
1 : Execute timer only if the document is the current front document

In a text frame, the remaining time of a defined period (e.g. the length of a lecture) can be shown. If the time is up, the text "End!" is displayed. After the timer was started with timer::frame_start (gFrame, "My Timer", 5.0);, the following script can be used for the corresponding layout rule (action with ClassID 36).

#include "internal/types.h"
int kTimeSpan = 1200; char kKey[] = "MMy timer";
int main () { char dt [512]; char now [512]; char str [512]; int sec = 0;
system::now (now, ddmmyyyy_hhmmss); timer::frame_get_start (gFrame, kKey, dt);
sec = kTimeSpan - system::time_diff (now, dt, ddmmyyyy_hhmmss);
sprintf (str, "%02d:%02d", sec/60, sec-((sec/60)*60)); if (sec < 0) { strcpy (str, "End!"); timer::frame_stop (gFrame, kKey); }
frame::replace (gFrame, str); return 0; }

v4.1.7, R26311
flags since v4.1.8 R27522

priint:comet InDesign® Plug-Ins, comet_pdf

timer::frame_stop
timer::frame_get_start

static int document_start(
  ItemRef ref,
  int actionID,
  float intervall,
  int flags = 0)

Install an idle-time handler to execute a script on a document. A frame may have any number of such timers. But please pay attention to short execution times. If the document is closed, active timers of this document are automatically removed.

Name Type Default Description
Return int   0 or ErrorCode

-1199 : Called by comet_pdf
ref ItemRef - Reference to the document. You can use any object (for example, gFrame) of the of the desired document here.

0 : No binding to a document (since v4.3 R34900)

Please note that in the 0 case, the functions timer::document_get_start and timer::document_stop must also be called with the document reference 0.

actionID int - ID of the action of the timer function. The action must have the ClassID 62. In addition to gFrame and gDocument, the following additional global variables are defined in the script:
  • char gDocumentID[]. The document ID of the timer document or empty ("")
  • int * gPendingCall. Set this variable to the value 1 (*gPendingCall = 1;) in the following cases:
    1. A global progress bar is shown
    2. The script requires a further call, e.g. if the script opens and edits a document in one step and closes this document again in a second step.
intervall float - Start/interval of the timer in seconds

<= 0.0 : Running the timer once
> 0.0 : Repeated execution of the timer at the specified interval

The minimum absolute length of the interval is 1 second!
flags int 0 Additional information for the timer

0 : no further details
1 : Execute timer only if the document is the current front document

The following script (save as action of ID 123456 and with ClassID 62) will write some log messages after every 10 seconds after it was started by timer::start (gFrame, 123456, 10.0);

#include "internal/types.h"
int main () { String nm = string::alloc (); String dt = string::alloc ();
document::name (nm, gDocument); system::now (dt, hhmmss); wlog ("", "#### Doc timer called for '%s' : %s\n", nm, dt);
return 0; }

v4.1.7, R26311
flags since v4.1.8 R27522
ref = 0 since v4.3 R34900

priint:comet InDesign® Plug-Ins, comet_pdf

timer::document_stop
timer::document_get_start

static int stop(char* label)

Stop timing of a cScript snippet. Take care to have balanced calls of timer::start and timer::stop for a given label.

Name Type Default Description
Return int   0 or ErrorCode

-1199 : Called by comet_pdf
label String oder char* - Label of timer used on start
⇨ Timer for frames
frameRef ItemRef - Destinated frame for the timer.
key String oder char* - Key of timer

Version 2.1 (P/R 770)

priint:comet InDesign® Plug-Ins, comet_pdf

timer::start
timer::get_start

static int frame_stop(ItemRef frameRef, char* key)

Stop a timer of a frame.

Name Type Default Description
Return int   0 or ErrorCode

-1199 : Called by comet_pdf
frameRef ItemRef - Destinated frame for the timer to stop.
key String oder char* - Key of timer

v4.1.7, R26311

priint:comet InDesign® Plug-Ins, comet_pdf

timer::frame_start
timer::frame_get_start

static int document_stop(ItemRef ref, int actionID)

Stop a timer of a document.

Name Type Default Description
Return int   0 or ErrorCode

-1199 : Called by comet_pdf
ref ItemRef - Reference to the document. You can use any object (for example, gFrame) of the of the desired document here.
actionID int - ID of the action executed by the timer

v4.1.7, R26311

priint:comet InDesign® Plug-Ins, comet_pdf

timer::document_start
timer::document_get_start

static int frame_get_start(
  ItemRef frameRef,
  char* key,
  char* out_startDateTime)

Get the start time of a frame timer.

Name Type Default Description
Return int   0 or ErrorCode

502 : No timer startet for the given frame and key
-1199 : Called by comet_pdf
frameRef ItemRef - valid frame reference
key String or char* - Key of timer
out_startDateTime String or char* 0 DateTime string (YYYYMMDDHHMMSS) of the start time. May be 0. If not 0 and when using char*, it must have 15 bytes allocated at least.

v4.1.7, R26311

priint:comet InDesign® Plug-Ins, comet_pdf

timer::frame_start
timer::frame_stop
system::time_diff

static int document_get_start(
  ItemRef ref,
  char* key,
  char* out_startDateTime)

Get the start time of a document timer.

Name Type Default Description
Return int   0 or ErrorCode

502 : No timer startet for the given frame and key
-1199 : Called by comet_pdf
ref ItemRef - Reference to the document. You can use any object (for example, gFrame) of the of the desired document here.
actionID int - ID of the action that the timer should perform
out_startDateTime String or char* 0 DateTime string (YYYYMMDDHHMMSS) of the start time. May be 0. If not 0 and when using char*, it must have 15 bytes allocated at least.

v4.1.7, R26311

priint:comet InDesign® Plug-Ins, comet_pdf

timer::document_start
timer::document_stop
system::time_diff

static int reset(char* label = 0)

reset all timer data

Name Type Default Description
Return int   0
label String or char* 0 timer label used on start

Version 2.1 (P/R 770)

priint:comet InDesign® Plug-Ins, comet_pdf

timer::start

static int dump(char* label)

dump all timer data to log file

Name Type Default Description
Return int   0
label String or char* - timer label used on start

Version 2.1 (P/R 770)

priint:comet InDesign® Plug-Ins, comet_pdf

timer::start

static float get_seconds(char* label, int what)

Get the time counted by a timer.

Name Type Default Description
Return float   Counted time or -1.0
label String or char* - Label of timer used on start
what int - What time to return?

0 : ΔTsAverage time needed for one time measurement itself.

1 : fastest
2 : fastest - ΔTs
3 : slowest
4 : slowest - ΔTs
5 : average
6 : average - ΔTs
7 : first
8 : first - ΔTs
9 : last
10 : last - ΔTs
11 : sum
12 : sum - ΔTs

Version 2.1, R 1678, 18. Nov. 2009

priint:comet InDesign® Plug-Ins, comet_pdf

timer::get_counter
timer::start
timer::stop
timer::dump

static int get_counter(char* label, int what)

Get a value counted by a timer.

Name Type Default Description
Return int   Counter value or -1
label String oder char* - Label of timer used on start
what int - Value to retrieve?

1 : number of calls
2 : maximum recursion depth
3 : stack

Version 2.1, R 1678, 18. Nov. 2009

priint:comet InDesign® Plug-Ins, comet_pdf

timer::get_seconds
timer::start
timer::stop
timer::dump

Time measurement of script parts. Enclose parts of your script in calls to start/stop_timer.


char stTimerPrefix[] = "XXXX";
int stTimerID = 0; char stTimerLabel [256];
char * timer_label () { sprintf (stTimerLabel, "%s %d", stTimerPrefix, stTimerID); return stTimerLabel; }
int start_timer () { ++stTimerID; timer::start (timer_label ()); return 0; }
int stop_timer () { float duration = 0.0; int hh = 0, mm = 0, ss = 0, milli = 0; timer::stop (timer_label ());
duration = timer::get_seconds (timer_label (),11); hh = toint (duration / 3600.0); mm = (toint (duration) % 3600) / 60; ss = toint (duration) % 60; milli = toint (duration * 1000.0 + 0.5) % 1000;
wlog("","%s stopped : %02d:%02d:%02d+%03d (%f)\n", timer_label (), hh, mm, ss, milli, duration);
timer::reset (timer_label ()); return 0; }
int main () { start_timer (); // // script statements // stop_timer ();
start_timer (); // // script statements // stop_timer ();
return 0; }

Author
Christoph Soergel
Version
16.10.2024, 11:40 Uhr
Since
Plugin Version 2.1

Alphabetic index HTML hierarchy of classes or Java