Tip

Optimize performance of your LotusScript 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)
    Options:
 Option Public
 Use "class.tools.lstimer"

    Declaration:
 Dim ticker as lstimer

    Initialize:
 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.

@If(@Environment("UserlsTimerMode")
= "0";
@Do(
@Environment("UserlsTimerMode";"1");
@Prompt([OK];"lsTimer ON";"lsTimer 
is now ON"));
@Do(
@Environment("UserlsTimerMode";"0");
@Prompt([OK];"lsTimer OFF";"lsTimer
 is now OFF")))
%END REM

' 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.tools.lstimer"
'' Class to measure execution time of 
methods and properties in a LotusScript
library.
' @see class.tools.debug
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 =  
Getthreadinfo
(LSI_THREAD_TICKS)
 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 
False
'@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
(LSI_THREAD_TICKS)
  If ( Iselement(ticker(Lsi_info(12))) = True ) 
Then
   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 
Integer
  If Not  ( Status = True ) Then Exit Function
  
  If ( Iselement(ticker(Lsi_info(12))) = True ) 
Then
   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 / 
ticksPerSecs()
    Print "ticker:" & Lsi_info(12) & ":" argREF
& "( " & Cstr(secs)  & " sec)"
   Else
    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 ) 
Then
   Print "ticker: " & Lsi_info(12) & " is 
running, could 
not erase timer"
   Me.erase = False
   Exit Function
  Else
   Erase ticker
   Me.erase = True
  End If
 End Function
 
'---Private methods ---  
 Private Function ticksPerSecs() 
As Integer
  ticksPerSecs = Getthreadinfo
(LSI_THREAD_TICKS_PER_SEC)
 End Function
End Class

MEMBER FEEDBACK TO THIS TIP

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 SearchDomino.com 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.

This was first published in June 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.