Manage Learn to apply best practices and optimize your operations.

Optimize performance of your LotusScript code

Ever wondered where the bottleneck is that makes you code so darn slow? With the lsTimer class below, you can easily pinpoint any time-consuming method piece of Lotus script code.

View member feedback to this tip.

Ever wondered where the bottleneck is that makes you code so darn slow? With the lsTimer class below, you can easily pinpoint any time-consuming method, property, event, agent or any other piece of LotusScript code.

Whenever you want to measure more than one method in the same scope, make the lsTimer class global.

Global (Whatever)
 Option Public
 Use ""

 Dim ticker as lstimer

 Sub Initialize
  Set ticker = New lsTimer()
 End Sub
End Global

Public Function myFunction()
    Call ticker.start()
 ' your code goes here
    Call ticker.stop()
    Call ticker.out(Lsi_info(12), True)
End function 
Code: %REM
Create a smart icon with the 
@functions below to easily toggle
 the lsTimer 
mode on and off.

= "0";
@Prompt([OK];"lsTimer ON";"lsTimer 
is now ON"));
@Prompt([OK];"lsTimer OFF";"lsTimer
 is now OFF")))

' include lsconst & lserr.
%INCLUDE "lsconst.lss"
%INCLUDE "lserr.lss"

Type recTicks
 startTick As Single
 stopTick As Single
 mode As Integer
End Type
Private Const LIB_PACKAGE = 
'' Class to measure execution time of 
methods and properties in a LotusScript
' @see
Public Class lstimer
 Private s As notessession
 Private status As Integer
 Private ticker List As recTicks
 Private tmpTick As Single
'' Construct new instance
' @param None
 Public Sub new()
  Set s = New notessession
  status = ( s.getEnvironmentString
( "UserlsTimerMode" ) = "1" )
 End Sub
'' Class destructor
' @param None
 Public Sub delete()
  Erase ticker
 End Sub
'' Start a new timer for the calling method
' @return Allways returns True 
' @see stop 
 Public Function start() As Integer
  Me.start = True
  ticker(Lsi_info(12)).mode = True
  ticker(Lsi_info(12)).stopTick = 0
  ticker(Lsi_info(12)).startTick =  
 End Function
''Stop timer function for the calling method. 
Stop() must be called prior 
to calling the out() method. 
'@return True if selected timer was 
stopped successfully otherwise return 
'@see start 
 Public Function stop() As Integer
  tmpTick = Getthreadinfo(LSI_THREAD_TICKS)
  If ( Iselement(ticker(Lsi_info(12))) = True ) Then
   tmpTick = tmpTick - ticker(Lsi_info(12)).startTick
   ticker(Lsi_info(12)).stopTick = ticker(Lsi_info
(12)).stopTick + tmpTick
   ticker(Lsi_info(12)).mode = False'
   Me.stop = True 
  End If 
 End Function
''Resume a previous stopped timer 
function for the calling method 
'@return True if resumed successfully 
otherwise return False
'@see stop 
 Public Function resume() As Integer
  tmpTick = Getthreadinfo
  If ( Iselement(ticker(Lsi_info(12))) = True ) 
   ticker(Lsi_info(12)).mode = True
   ticker(Lsi_info(12)).startTick =  tmpTick
   Me.resume = True
  End If
 End Function 
''Print the total execution time for the 
calling method
'@param argREF The name of the calling 
method ( use Lsi_info(12) )
'@param argMode If True the execution time 
is calculated in seconds otherwise 
it is calculated in ticks  
'@return True if output was successfully 
otherwise return False if the timer 
for the calling method is running
'@see stop
 Public Function out(Byval argREF 
As String, argMode As Integer) As 
  If Not  ( Status = True ) Then Exit Function
  If ( Iselement(ticker(Lsi_info(12))) = True ) 
   out = True
   If ( ticker(Lsi_info(12)).mode = True ) Then
    Print "ticker: " & Lsi_info(12) & " is running"
    out = False
    Exit Function
   End If
   If ( argMode = True ) Then
    Dim secs As Single
    secs = ticker(Lsi_info(12)).stopTick / 
    Print "ticker:" & Lsi_info(12) & ":" argREF
& "( " & Cstr(secs)  & " sec)"
    Print "ticker:" & Lsi_info(12) & ":" argREF
& "( " & Cstr(ticker(Lsi_info(12)).stopTick)  
& " tick)"
   End If 
  End If
 End Function
'' Erase timer for calling methods
'@return True if timer was successfully 
erased otherwise return False
 Public Function erase() As Integer
  If ( ticker(Lsi_info(12)).mode = True ) 
   Print "ticker: " & Lsi_info(12) & " is 
running, could 
not erase timer"
   Me.erase = False
   Exit Function
   Erase ticker
   Me.erase = True
  End If
 End Function
'---Private methods ---  
 Private Function ticksPerSecs() 
As Integer
  ticksPerSecs = Getthreadinfo
 End Function
End Class


This system would appear to be an incredibly complex way of determining execution time. I use the following three lines of code:

Dim start as Double 
start = Timer() 
' your code goes here
Print ( Timer() - start ) 

The Timer function is accurate to 1/100 of a second. Anything shorter than that would be unapparent to the user and therefore not worth measuring. Multiple measurement points may be calculated by adding other double variables as needed.

What are the advantages, if any, of using Jens Nielsen's method?

—Jack S.

Do you have comments on this tip? Let us know.

This tip was submitted to the tip exchange by member Jens Nielsen. Please let others know how useful it is via the rating scale below. Do you have a useful Notes/Domino tip or code to share? Submit it to our monthly tip contest and you could win a prize and a spot in our Hall of Fame.

Dig Deeper on LotusScript

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...