I am a programmer for a small web development company, using classic ASP
(on IIS 6). The question has arisen as to whether a test of the form
'Len(x)=0' executes faster than 'x=""'.

My initial guess was that 'x=""' would be faster than 'Len(x)=0', but a
colleague disagrees.

I very quickly knocked up the following test script, which is followed
by the results I obtained from three runs.

===

s1 = ""
s2 = "hello"
t1 = now()
for i = 1 to 10000000
if s1 = "" then j = 1
if s2 = "" then j = 2
next
t2 = now()
for i = 1 to 10000000
if Len(s1) = 0 then j = 1
if Len(s2) = 0 then j = 2
next
t3 = now()
wscript.echo (t2-t1)*84600, (t3-t2)*84600

===

C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

12.7291671800776 21.5416661623749

C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

11.749999668973 21.5416667779209

C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
Microsoft (R) Windows Script Host Version 5.6
Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.

11.750000284519 21.5416667779209

C:\Documents and Settings\Nick.BRAM-2\Desktop>

===

This suggests that 'Len(x)=0' is actually quite a bit slower than
'x=""'. Am I wrong? Have I missed something vital? Is this an issue for
which there has been any discussion on the Internet (and if so, where)?
If so, is there a general consensus one way or the other?

Answers to any of these questions would be greatly appreciated!

PS: I have spotted one mistake -- that I should have used 86400 instead
of 84600 -- but unfortunately it's irrelevant.

--
Nick Roberts

Re: Fastest way to test for empty string by Bart

Bart
Wed Mar 28 15:15:22 CDT 2007

This newsgroup is not only informative but FUN, too. Since Len() is an
internal function that is called, I would expect it to be a little slower
since other lines of code must run to evaluate the expression. There is also
more binary math going on for the longer evaluation.

I personally like to Trim() things before I validate them. In this
particular example I would trim them both, which would probably give you the
same results.

I don't think you missed anything, other than 15 minutes of work....like me.
=]

"Nick Roberts" <nick.roberts@acm.org> wrote in message
news:0KzOh.99964$MR6.29402@fe1.news.blueyonder.co.uk...
>I am a programmer for a small web development company, using classic ASP
>(on IIS 6). The question has arisen as to whether a test of the form
>'Len(x)=0' executes faster than 'x=""'.
>
> My initial guess was that 'x=""' would be faster than 'Len(x)=0', but a
> colleague disagrees.
>
> I very quickly knocked up the following test script, which is followed by
> the results I obtained from three runs.
>
> ===
>
> s1 = ""
> s2 = "hello"
> t1 = now()
> for i = 1 to 10000000
> if s1 = "" then j = 1
> if s2 = "" then j = 2
> next
> t2 = now()
> for i = 1 to 10000000
> if Len(s1) = 0 then j = 1
> if Len(s2) = 0 then j = 2
> next
> t3 = now()
> wscript.echo (t2-t1)*84600, (t3-t2)*84600
>
> ===
>
> C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
> Microsoft (R) Windows Script Host Version 5.6
> Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
>
> 12.7291671800776 21.5416661623749
>
> C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
> Microsoft (R) Windows Script Host Version 5.6
> Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
>
> 11.749999668973 21.5416667779209
>
> C:\Documents and Settings\Nick.BRAM-2\Desktop>test1.vbs
> Microsoft (R) Windows Script Host Version 5.6
> Copyright (C) Microsoft Corporation 1996-2001. All rights reserved.
>
> 11.750000284519 21.5416667779209
>
> C:\Documents and Settings\Nick.BRAM-2\Desktop>
>
> ===
>
> This suggests that 'Len(x)=0' is actually quite a bit slower than 'x=""'.
> Am I wrong? Have I missed something vital? Is this an issue for which
> there has been any discussion on the Internet (and if so, where)? If so,
> is there a general consensus one way or the other?
>
> Answers to any of these questions would be greatly appreciated!
>
> PS: I have spotted one mistake -- that I should have used 86400 instead of
> 84600 -- but unfortunately it's irrelevant.
>
> --
> Nick Roberts
>



Re: Fastest way to test for empty string by Anthony

Anthony
Thu Mar 29 03:14:43 CDT 2007


"Nick Roberts" <nick.roberts@acm.org> wrote in message
news:0KzOh.99964$MR6.29402@fe1.news.blueyonder.co.uk...
> I am a programmer for a small web development company, using classic ASP
> (on IIS 6). The question has arisen as to whether a test of the form
> 'Len(x)=0' executes faster than 'x=""'.
>
> My initial guess was that 'x=""' would be faster than 'Len(x)=0', but a
> colleague disagrees.

If this really mattered you wouldn't be using a script language.