Abap - Calculate the runtime/elapsed time

By xngo on July 10, 2019

REPORT  Z_GET_RUNTIME.
* Note: GET RUN TIME returns microseconds. If the elapse time is too long, it may overflow the integer data types.
 
DATA: T1 TYPE I,
      T2 TYPE I,
      ELAPSED TYPE I.
 
* Get the time and put in T1.
GET RUN TIME FIELD T1.
 
* Wait 3 seconds.
CALL FUNCTION 'ENQUE_SLEEP'
  EXPORTING
    SECONDS = 3.
 
* Get the time and put in T2.
GET RUN TIME FIELD T2.
 
* Calculate the different between T2 and T1.
ELAPSED = T2 - T1.            " In microseconds.
ELAPSED = ELAPSED / 1000000.  " In seconds.
 
* Display the runtime.
WRITE:/ ' Runtime =', ELAPSED, 'seconds'.

Reference: http://help.sap.com/abapdocu_702/en/abapget_run_time.htm

REPORT  Z_ELAPSED_TIMESTAMP.
* Get the elapsed time using timestamps.
 
DATA: LV_TIMESTAMP_START TYPE TIMESTAMPL,
      LV_TIMESTAMP_END TYPE TIMESTAMPL,
      LV_TIMESTAMP_ELAPSED TYPE TIMESTAMPL,
      LV_ELAPSED TYPE STRING.
 
* Get the start time stamp.
GET TIME STAMP FIELD LV_TIMESTAMP_START.
 
********************************************
* Wait 11 seconds.
CALL FUNCTION 'ENQUE_SLEEP'
  EXPORTING
    SECONDS = 11.
 
 
* Get the end time stamp.
GET TIME STAMP FIELD LV_TIMESTAMP_END.
 
* Output the time stamps.
WRITE: / 'Start   : ', LV_TIMESTAMP_START.
WRITE: / 'End     : ', LV_TIMESTAMP_END.
 
* Calculate the elapsed time.
LV_TIMESTAMP_ELAPSED = LV_TIMESTAMP_END - LV_TIMESTAMP_START.
LV_ELAPSED = |{ LV_TIMESTAMP_ELAPSED TIMESTAMP = ISO TIMEZONE = 'UTC   ' }|.
 
* Output elapsed time stamp.
CONCATENATE LV_ELAPSED+11(10) '' INTO LV_ELAPSED. " Extract the time only.
WRITE: / 'Runtime : ', LV_ELAPSED.

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.