1. Returning the var type based on content of object
- Posted by znorq2 May 13, 2009
- 862 views
Hi,
I created the following function;
------------------------------------------------------------------------------------------------ global function fVarType(object oObject) -->> cGA08, uGA08 ------------------------------------------------------------------------------------------------ -->> Purpose: Return A, N, F or E depending on the oObject content; -->> 'A'tom -->> 'N'ested sequence (Array) -->> 'F'lat sequence (May be considered as a string) -->> 'E'mpty sequence -->> ========================================================================================= -->> GA08; Created. -->> ========================================================================================= -- 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
Can someone please verify if this is an efficient way to do it - or can I speed this up?
Kenneth / ZNorQ
2. Re: Returning the var type based on content of object
- Posted by DerekParnell (admin) May 13, 2009
- 857 views
Hi,
I created the following function;
I suggest you actually benchmark this before deciding which is faster.
------------------------------------------------------------------------------------------------ global function fVarType(object oObject) -->> cGA08, uGA08 ------------------------------------------------------------------------------------------------ -->> Purpose: Return A, N, F or E depending on the oObject content; -->> 'A'tom -->> 'N'ested sequence (Array) -->> 'F'lat sequence (May be considered as a string) -->> 'E'mpty sequence -->> ========================================================================================= -->> GA08; Created. -->> ========================================================================================= -- atom xType -- -- if atom(oObject) then return 'A' end if if length(oObject) = 0 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 return 'N' end if end for -- return 'F' -- end function
3. Re: Returning the var type based on content of object
- Posted by znorq2 May 13, 2009
- 876 views
Hi,
I created the following function;
I suggest you actually benchmark this before deciding which is faster.
------------------------------------------------------------------------------------------------ global function fVarType(object oObject) -->> cGA08, uGA08 ------------------------------------------------------------------------------------------------ -->> Purpose: Return A, N, F or E depending on the oObject content; -->> 'A'tom -->> 'N'ested sequence (Array) -->> 'F'lat sequence (May be considered as a string) -->> 'E'mpty sequence -->> ========================================================================================= -->> GA08; Created. -->> ========================================================================================= -- atom xType -- -- if atom(oObject) then return 'A' end if if length(oObject) = 0 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 return 'N' end if end for -- return 'F' -- end function
Hi Derek, thx for the feedback.
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..
Kenneth / ZNorQ
4. Re: Returning the var type based on content of object
- Posted by mattlewis (admin) May 13, 2009
- 877 views
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
5. Re: Returning the var type based on content of object
- Posted by znorq2 May 18, 2009
- 852 views
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.
Matt
Matt,
So, counting downards without the for's "by -1" parameter, results in the code block not being executed at all? I guess that is evaluated before the for loop starts the execution?
Kenneth / ZNorQ Learing something new every day..
6. Re: Returning the var type based on content of object
- Posted by DerekParnell (admin) May 18, 2009
- 879 views
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.
Matt
Matt,
So, counting downards without the for's "by -1" parameter, results in the code block not being executed at all? I guess that is evaluated before the for loop starts the execution?
Kenneth / ZNorQ Learing something new every day..
Sort of...
Given this code ...
for cnt = 2 to length(oObject) do if sequence(oObject[cnt]) then return 'N' end if end for
The body of the 'for' loop is only executed when 'cnt' is less than or equal to the length of 'oObject'. So if 'cnt' starts at 2 and the length is 1, then the body will not execute because 'cnt' is not less than or equal to the length.
It has nothing to with counting up or down.
7. Re: Returning the var type based on content of object
- Posted by znorq2 May 18, 2009
- 831 views
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.
Matt
Matt,
So, counting downards without the for's "by -1" parameter, results in the code block not being executed at all? I guess that is evaluated before the for loop starts the execution?
Kenneth / ZNorQ Learing something new every day..
Sort of...
Given this code ...
for cnt = 2 to length(oObject) do if sequence(oObject[cnt]) then return 'N' end if end for
The body of the 'for' loop is only executed when 'cnt' is less than or equal to the length of 'oObject'. So if 'cnt' starts at 2 and the length is 1, then the body will not execute because 'cnt' is not less than or equal to the length.
It has nothing to with counting up or down.
Ok. I just realized how many if-then blocks of code I could have omitted using this technique. Anyway, great help from the both of you - thanks.
Kenneth / ZNorQ