What's the difference between Vartype and Type
Happy New Year… I took some time off to spend with my family.
To get the type of variable “MyVar”, you can use TYPE(“MyVar”) or VARTYPE(MyVar). Notice no quotes are used for VARTYPE.
TYPE(cExp) scans and compiles the expression in the string parameter. For example, for TYPE(“3+4”), VFP will compile the expression in the string parameter, evaluate it, then return the type of the result. Putting it in a loop causes the string to be compiled each time. It has been in the language since foxbase days
VARTYPE(eExp) will just return the type of the value at the top of the Fox expression stack. There is no extra compilation step and the expression is evaluated just as any VFP expression before the VARTYPE routine gets called to return the type of the value at the top of the stack. VARTYPE was added around VFP6.
Which do you think is faster?
ns=SECONDS()
zz="a"
FOR i = 1 TO 1000000
* cc=TYPE("zz")
cc=VARTYPE(zz)
ENDFOR
?SECONDS()-ns
Comments
- Anonymous
January 03, 2006
I always thought TYPE was faster than VARTYPE, but I can't remember why cause you'd think it would be slower. Also, FYI for other readers of this blog, TYPE will not throw an error if it doesn't evaluate to an actual var (will return "U"), but VARTYPE will throw an error if var does not exist. - Anonymous
January 03, 2006
Hehe, should have ran the test before answering. Of course VARTYPE is faster! - Anonymous
January 03, 2006
Actually vartype only seems to run into problem with array members that don't exist
release abcd
?Vartype(abcd)
returns 'U'
but vartype(temp[2])
causes an error. - Anonymous
January 04, 2006
The comment has been removed - Anonymous
January 04, 2006
The comment has been removed - Anonymous
January 04, 2006
The comment has been removed - Anonymous
January 04, 2006
The comment has been removed - Anonymous
January 05, 2006
VARTYPE() is better when you know that the var exist and just want to know the data type (for example: proc parameters).
TYPE() is usefull when you need to know if a var or property exist, but in the case of properties PEMSTATUS() is faster than TYPE().