Question

I would like to know if the performance is better in case I am storing the string length in a temporary variable when I am using a loop of at list 10,000 loops instead of calculating it in every loop?

Code example calculating the string length:

'--
Dim I
Dim str : str = "Lets say this string length is around 50,000"
'--
For I=0 To 100000
    Response.Write Len(str) & "<br>"
Next

Code example using a temporary variable:

'--
Dim I
Dim str : str = "Lets say this string length is around 50,000"
Dim temp : temp = Len(str)
'--
For I=0 To 100000
    Response.Write temp & "<br>"
Next

What do you think is better?

Thanks!

Was it helpful?

Solution

Here's an idea: Why don't you just measure it?

Dim start: start = Timer
...<code here>...
Response.Write FormatNumber(Timer - start, 8)

To measure is to know. Having said that: You might want to read the story about Shlemiel the Painter.

Eventually the Response.write will probably be the most expensive operation; if you really want to measure the "cost" of Len() vs. a temp variable (even though both methods output the same amount of data) you should probably do something like this:

Dim I, J, X
Dim start

Dim str : str = String(50000, "-")
Dim tmp : tmp = Len(str)

Response.Write "Temp var:<br>"
For J = 0 to 10
    start = Timer
    X = 0
    For I=0 To 100000
        x = x + tmp
    Next
    Response.write FormatNumber(Timer - start,8) & "<br>"
Next

Response.Write "Len:<br>"
For J = 0 to 10
    start = Timer
    X = 0
    For I=0 To 100000
        x = x + Len(str)
    Next
    Response.write FormatNumber(Timer - start,8) & "<br>"
Next

In my case, the output is:

Temp var:
0,03125000
0,03125000
0,04687500
0,03125000
0,03515625
0,02734375
0,03125000
0,03125000
0,03515625
0,02734375
0,03125000
Len:
0,04687500
0,04687500
0,03125000
0,04687500
0,04687500
0,04687500
0,05078125
0,02734375
0,04687500
0,05078125
0,04687500

As you can see, using Len() every iteration is measureably slower (even though it isn't that much, about 35%). Using a temp-variable will be a good idea, but you could've guessed that without this knowledge or measuring at all, didn't you?

I am unsure about VBScript but many languages (and their compilers/JIT'ers) do optimize this stuff at compile/JIT-time on their own. The compiler/runtime/JIT usually "notices" these kinds of patterns and will introduce a "temp variable" automatically.

But then: why would you be doing tens-of-thousands of iterations on such a large chunk of data in (classic) ASP at all? Do you really have to output this much data? Can't the processing be done (more) by the backend or something? Are you doing actually 10.000 iterations on 50Kb strings? Or is this just a theoretical discussion? If you're doing one, or a handful, iterations, on "simple, short strings" I wouldn't bother with an extra variable at all. This introduces other issues; for example having to keep the variable "in sync" should the str variable change etc. Also readability (and "understandability) would be improved just using Len(foo) where you need the length. If you do introduce a variable, which is justified and sometimes an optimization worth the "hassle", I would at least make sure it is named correctly (not tmp but foolength for example).

OTHER TIPS

If you just follow the simple rule "Never do in a loop what you can do before it" you don't have to waste time on benchmarks.

(It's bitter to have to read such a question, and I want to batter a programmer who does not know on which side the toast is buttered.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top