Skip to main content
    Country/region select      Terms of use
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

Bob Balaban writes:

In LotusScript, instead of testing for a null string with:
if s = "" or s <> ""
It's much faster to do it this way:
if len(s) = 0

I have heard this often, and I was curious about the actual difference. So, I decided to test it out. Bottom line: I wouldn't bother.

The clock on a Windows PC only "ticks" every 100th of a second, so to get an accurate timing on something you have to do it a bunch of times. I like to wait until the timer "ticks over" and then start repeatedly doing whatever it is I'm timing, counting how many times I can do it in a pre-selected amount of time. Here's the subroutine I modify to do the test.

Function TimeControl(Byval limit As Single) As Single
     Dim startTime As Single, endTime As Single

     Dim iters As Long

' do your initialization for the test here

' ...

     startTime = Timer

     While startTime = Timer

             ' wait for the next timer tick


     endTime = Timer + limit


             iters = iters + 1

' put the code you want to time here.

' ...

     Loop While Timer < endTime

     TimeControl = (Timer-startTime) / iters ' return time per iteration

End Function

We should get a pretty accurate measure provided limit is at least 4 (seconds) or so. If the task takes a lot longer than we expect and the final iteration goes past the limit, it uses the actual elapsed time to calculate the time per iteration.

Since the mechanics of looping and checking the timer takes a little time, I like to call this unmodified function as a control, and subtract the time it returns from the time for whatever I'm testing. So I might write:

Dim control As Single, tOpenDoc As Single
control = TimeControl(5.)

tOpenDoc = TimeOpenDoc(5.)-control

It's best to run the test several times, throw out high/low values and average the rest, to account for differences in how busy your system might be at the time the test is running. Using this technique, I timed 5 things:
Operation: Time (sec)
Test string with = when string is blank .00000058
Test string with = when string is not blank .00000058
Test string with Len(x) = 0 when string is blank .00000035
Test string with Len(x) = 0 when string is not blank .00000047
GetDocumentByID in a local database .00020882

Using Len is faster, but the difference is only about two ten-millionths of a second -- if doing five million string comparisons, you'd see a one-second difference in your code's execution time. Note that the constant "" is built-in to the LotusScript engine, so there is no question of allocating memory.

Most of the time in a typical script is spent opening databases and documents, reading and setting document field values, saving documents, navigating views and collections, and other such operations that take hundreds to millions of times as long as a string comparison. Given that, the amount of time you save by using Len is not noticeable, so it's better to maximize readability/maintainability. For me, the equal test is more readable. If you take the developer time you would spend changing every = "" to a test of Len, and apply that to eliminating unneeded database operations instead, you'll get way more bang for your performance-optimization buck.

Andre Guirard | 27 February 2008 02:56:00 AM ET | Home, Plymouth, MN, USA | Comments (3)


1) Performance testing technique, and what makes enough difference to bother with
Charles Robinson | 2/27/2008 9:18:55 AM

Thanks for taking the time to do the test and share the results. This is very similar to the difference between GetItemValue and using extended item syntax. Maybe you could do a Lotusphere session on educating people on improvements and enhancements in the core languages that counter widely held beliefs regarding performance optimization. :-)

2) Performance testing technique, and what makes enough difference to bother with
Kerr | 2/27/2008 10:27:10 AM

There is clearly no point in spending time re factoring code to gain insignificant improvements like this. However when writing new code you still have to decide if you are going to write:

if foo=""


if len(foo) = 0

In the Java world I've been using foo.length()==0 rather than foo.equals("") for a while. In this case the speed increase has no trade off (IMHO) for readability or convenience.

ultimately it comes down to a style issue.

3) Performance testing technique, and what makes enough difference to bother with
Rob Goudvis | 3/3/2008 4:31:34 AM

After the message from Bob that you quoted, I did a similar test, because I too was curious about how much time would be saved using the different methods.

I did it much simpler with the following steps:

a) save current time

b) do a test loop

c) save the current time

d) display the timedifference

The result was that the comparison with an empty string took me 6 seconds and the test with the len-function took 4 seconds.

I did the test several times to rule out external influences. They all gave the same result.

I agree that Andre's method is more accurate and I will surely modify my code to use his approach.

Half a year ago I did some other time testing. I compared the processing of documents using Lotusscript against the same processing of documents via C++ code located in a DLL.

It showed that the times were exactly the same! I did a run that took about 200 seconds to execute. In this test I could handle 200.000 documents. Another run took 100 seconds and I could handle 100.000 documents.

The next thing I did was comparing executable code; in my case I took 2 nested loops with an integer loop-variable and a multiplication of these variables in the heart of the loop.

Here C++ showed a much better performance. The test took about 100 seconds in Lotusscript and in the C++ variant less than an single second. I did not bother to delve deeper into this.

 Add a Comment
Comment:  (No HTML - Links will be converted if prefixed http://)
Remember Me?     Cancel

Search this blog 


    About IBM Privacy Contact