Re: Returning the var type based on content of object
- Posted by mattlewis (admin) May 13, 2009
- 876 views
znorq2 said...
There is one question; Why doesn't this fail when there is a string format with only one character, i.e. "x"...? The for "cnt = 2 to..." shouldn't be able to continue, as there is no second element in a "x" string..
The for loop never executes, so execution jumps right to the final return statement.
Taking advantage of eu 4.0's inlining capability, I split the nested check out to its own function. The advantage here is that we don't have to check to see if oObject is a sequence before trying to subscript it. This is really only an advantage when interpreted. Translated code doesn't do all the checks that interpreted code does.
I get these results with eui.exe r2005:
ZNorq: 6.031 Derek: 5.86 Matt: 5.64Here is the code I used (of course, the type of data passed has a huge effect on the results):
global function znorq(object oObject) atom xType xType = 'F' if atom(oObject) then return 'A' end if if equal(oObject, "") then return 'E' end if for cnt = 1 to length(oObject) do if sequence(oObject[cnt]) then xType = 'N' exit end if end for return xType end function global function derek(object oObject) -->> cGA08, uGA08 atom xType xType = 'F' if atom(oObject) then return 'A' end if if equal(oObject, "") then return 'E' end if if sequence(oObject[1]) -- Most nested sequences nest on the 1st element. then return 'N' end if for cnt = 2 to length(oObject) do if sequence(oObject[cnt]) then xType = 'N' exit end if end for return xType end function with inline 60 function is_nested( sequence s, integer len = length(s) ) for cnt = 2 to len do if sequence(s[cnt]) then return 1 end if end for return 0 end function global function matt(object oObject) -->> cGA08, uGA08 if atom(oObject) then return 'A' end if if equal(oObject, "") then return 'E' end if if sequence(oObject[1]) -- Most nested sequences nest on the 1st element. then return 'N' end if if is_nested( oObject ) then return 'N' else return 'F' end if end function constant DATA = { 1, 1.5, "", "sfdsdfdsfsdf", { {}, 3, {}}, {1, 3.0, "hello"} } constant REPS = 5000000 atom zt, dt, mt object void zt = time() for i = 1 to REPS do for j = 1 to length( DATA ) do void = znorq( DATA[j] ) end for end for zt = time() - zt dt = time() for i = 1 to REPS do for j = 1 to length( DATA ) do void = derek( DATA[j] ) end for end for dt = time() - dt mt = time() for i = 1 to REPS do for j = 1 to length( DATA ) do void = matt( DATA[j] ) end for end for mt = time() - mt printf(1, "ZNorq: %g\nDerek: %g\nMatt: %g\n", {zt, dt, mt})
Matt