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 (8)

  1. Randy Jean says:

    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.

  2. Randy Jean says:

    Hehe, should have ran the test before answering. Of course VARTYPE is faster!

  3. don dodge says:

    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.

  4. Randy Jean says:

    "Actually vartype only seems to run into problem with array members that don’t exist"

    Whoa! You are so right! When did this change? I could have swore that VARTYPE used to blow up if the var didn’t exist. I have a lot of old code using TYPE vs. VARTYPE because I thought it was "safer" – usually it was to test for an object to be type "O" before calling PEMSTATUS, etc. Hmmm… Come to think of it, maybe I’m thinking of PEMSTATUS vs. TYPE on the performance thing too…

    – Randy

  5. Randy Jean says:

    Interesting… I just did a code reference for TYPE(" vs. VARTYPE( in the latest Visual Fox Express (VFE2005) classes – interestingly, there are still 752 occurrences of TYPE() but there are 751 occurrences of VARTYPE() – WHY? Looking closer it seems mostly TYPE is used as an alternative to PEMSTATUS – obvioulsy, it’s got to be quicker to make one TYPE() call vs. testing for an object using TYPE() then checking for a property on that object using PEMSTATUS. But, if VARTYPE() doesn’t bomb out on an object that doesn’t "exist" wouldn’t that make the code much faster? I think I need to talk to the Feltmans about this…

  6. Randy Jean says:

    OK, finally went and checked the fox wiki on this:

    http://fox.wikis.com/wc.dll?Wiki~VarType~VFP

    Vartype will error (alias not found) on a non-existent object when checking for a property. That’s what I was thinking of.

    However, which of these is faster?

    ?VARTYPE(oooo) = "O" AND VARTYPE(oooo.xxxx) <> "U"

    or

    ?TYPE("oooo.xxxx") <> "U"

    ?

  7. Randy Jean says:

    <code>

    ns=SECONDS()

    * oooo = CREATEOBJECT("empty")

    FOR i = 1 TO 1000000

    * lx = VARTYPE(oooo) = "O" AND VARTYPE(oooo.xxxx) <> "U"

    * lx = VARTYPE(oooo) = "O" AND PEMSTATUS(oooo,"junk",5)

    lx = TYPE("oooo.xxxx") <> "U"

    ENDFOR

    ?SECONDS()-ns

    </code>

    Try the above code with and without the object existing. Bottomline, I’m going to be avoiding the use of TYPE() as often as I can now, especially where I know there will potentially be many calls to it.

  8. Fernando D. Bozzo says:

    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().