1

I was using a code-parser that checks for syntax errors and such, running through some old code, and saw the following suggestion:

Str functions should use $( for speed: 
Trim(

I figure that using the '$' enforces strong typing (so we know it's going to be a string), but how much time does that really save?

So the question is: what extra steps happen behind the scenes without the '$' in place and how expensive are these extra steps?

It's pretty much guaranteed to be negligible these days, but I'm curious as to the relative cost; if not having the '$' costs 4 ticks, but having '$' costs 1 tick, then that's a pretty significant increase in relative performance.

elrobe
  • 555
  • 2
  • 16
  • 1
    I am almost inclined to say it is a Possible duplicate of https://stackoverflow.com/a/36555495/11683, but given that you want to know exactly how much it saves you, I cannot really. It saves you [packing the result into a Variant](https://stackoverflow.com/a/13050787/11683), but I do not know how much that is in ticks. In any case you should choose the `$`-ed or `$`-less version depending on which one is correct to use (i.e. matches the source/destination types). – GSerg Apr 06 '18 at 20:09
  • Right, thanks for the link, though. I'm mainly curious how much time it saves, even if it's some relative estimate. – elrobe Apr 06 '18 at 20:10
  • You can get the `TimeBefor = Now()` befor you loop 100,000 of $ then `TimeAfter = Now()` then do the same without the $ – Siyon DP Apr 09 '18 at 09:35

1 Answers1

2

VB6 has two versions of string functions. "Normal" string functions return Variant datatypes. The functions with $ return String datatypes. Using strings ist always faster than using variants. Because of this, you should always use $-functions.

Vallen
  • 61
  • 6
  • But why is it faster to return strings than variants? Variants take up more space if I remember correctly, so how much extra processing goes into creating the variant rather than a string and then turning the variant into a string anyway? – elrobe Apr 08 '18 at 22:38
  • 1
    My experience tells me that on modern hardware, the speed difference between `Str` and `Str$` is _insignificant_. VB6 was written for computer platforms that are 20 years old by now. There is vastly more power available now to run your VB6 program. You are far better off spending your time to optimize your data access, queries, and algorithms. Don't waste time on things that matter very little. – kismert Apr 09 '18 at 19:25
  • 1
    @kismert Certainly it's almost insignificant right now, and probably it also was when it was just released. But given that there is an optimization (for both speed and memory usage) by simply adding a single character, why not do it right away when writing code? I wouldn't seek all missing of them, but if I happen to find one or write a new one, I can't find a reason not to put a $. – Alejandro Apr 11 '18 at 20:07
  • @Alejandro -- I say no because (1) Code reads cleaner without the $. (2) Even if you added the $ to 10000 string functions in your project, your customer will _never_ notice the difference. So why? Put that keystroke to better use :) – kismert Apr 11 '18 at 23:25
  • @elrobe: If you use $-functions, the result is a string. But if you use standard functions without $ at the end, the result is a variant, which has internally to be converted to a string. This needs time and because of this $-functions are always faster. – Vallen Apr 16 '18 at 19:49
  • @kismert: You are right, code reads cleaner without $ at the end. But if you really have to use VB6, you should always use the most optimized code. You and your customer will not realize if a single command is fast or slow. But if you have a complex loop inside another loop, you will notice the difference. Even if today´s hardware is much faster than 20 years ago, there´s no need to waste cpu cycles. – Vallen Apr 16 '18 at 19:49
  • 1
    @Vallen: During compile, if the variable is a String, VB6 could easily 'add the $' by using the strongly typed function. This would erase any supposed performance advantage. Are you certain this is not the case? I say write some test code, set up some timers, and tell us what the difference really is. VBA could be a different story, because it is interpreted, not compiled like VB6 is. – kismert Apr 16 '18 at 21:46