In my testing, If strTest = "" took 7% less time than If Len(strTest) = 0
[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
GetTickCount is actually only accurate to about 10 - 15ms on XP/nt/2000/2003 (e.g. its 15.625ms on my PC)
Faster alternatives:
1ms: multimedia timers, eg timeGetTime (although you may need to play around timeBeginPeriod and timeEndPeriod to ensure this, depending on your OS)
Better than 1ms: QueryPerformanceCounter/QueryPerformanceFrequency - high performance timers. Resolution is dependant on your hardware - on my AMD2800 I get a resolution of approx 280ns (yes, that's nanoseconds)
It is probably worth pointing out that at modern PC speeds even an accuracy of 1ms wouldn't be enough to measure the difference on 100 iterations. But using the high performance timers, we can just start seeing that rmikesmith's assertion is correct (as to whether the speed difference is important enough to worry about in a real world application ... I'd say not)
thread222-1089225 and thread222-495997 might be of interest. There are several additional links in those threads
The original question was what is the best way to check for an empty string, not what is the fastest way.
I believe - and my own tests of millions of iterations have borne this out - that Mike is correct to say that checking for a length of zero is faster than checking explicitly against an empty string.
I would, however, suggest that checking against an empty string is clearer in most circumstances and would say that was probably the best.
Enjoy,
Tony
-------------------------------------------------------------------------------------------- We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at VBAExpress[
Oh I certainly was not disputing that Len = 0 is faster. I did get a difference but it seemed so small that I was wondering if it was within some sort of margin of error.
I also agree with Tony in that there may be something to say for the clarity of checking the contents, rather than the size of the container. Especially as the speed issue is (generally) not really much of a concern.
Didn't intend to steer this thread into an esoteric technical discussion, but the side trips are often quite interesting. Strongm, thanks for the very good info on timers. I agree with Tony that the strVar = "" test is probably the best in all but the most extreme situations. It is the form I almost always use.
BTW, my source for the speed comparison is the excellent reference VBA Developer's Handbook by Ken Getz & Mike Gilbert.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.