Re: absolute function
- Posted by Vinoba Mar 21, 2011
- 3701 views
Testing abs() function
I did extensive testing of the abs function for a single element integer, single element double value, a sequence of 1000 integers and a sequence of 1000 doubles.
The loop tested was had 1000000 iterations. Loop overhead was deducted from final results.
The following suggestion of mine was tested against the current system's abs function found in include\std\math.e
-- Method A return s + ( s * -2 * ( s < 0 ) )
I also tested the following code which was suggested by somebody in the past.
-- Method B return s * ((s > 0) - (s < 0))
Conclusions:
1. Method B CONSISTENTLY proved better than Method A
2. The system's current method, however clumsy it might look, CONSISTENTLY proved better than my proposed Method A, and the better proposed Method B.
3. The interpreted Euphoria results and compiled exe files gave nearly similar results, with the exe files doing only slightly better.
4. There is definitely NO SPEED GAIN by adopting either of the two changes suggested.
.. .. .. .. .. .. .. .. .. ..
Please note that the ratio numbers indicate time taken - therefore a number of greater than 1 means "slower" for the new changes suggested by me. .. .. .. .. .. .. .. .. .. ..
-- Method A New means Alternative abs - Exist means existing Abs fuction Time for execution 1000000 iterations using interpreted Euporia 4.01 Integer - 1 element: New, Exist, New/Exist ratio{0.047, 0.017, 2.764705882} Double - 1 element: New, Exist, New/Exist ratio{0.126, 0.032, 3.9375} Integer - 1000 elements: New, Exist, New/Exist ratio{25.282, 17.172, 1.472280457} Double - 1000 elements: New, Exist, New/Exist ratio{74.454, 38.36, 1.94092805}
.. .. .. .. .. .. .. .. .. ..
-- Method A compiled New means Alternative abs - Exist means existing Abs fuction Time for execution of 1000000 iterations using exe file created under Euporia 4.01 Bind Integer - 1 element: New, Exist, New/Exist ratio{0.047, 0.047, 1} Double - 1 element: New, Exist, New/Exist ratio{0.125, 0.047, 2.659574468} Integer - 1000 elements: New, Exist, New/Exist ratio{25.36, 17.062, 1.486343922} Double - 1000 elements: New, Exist, New/Exist ratio{83.828, 35.093, 2.388738495}
.. .. .. .. .. .. .. .. .. ..
-- Method B New means Alternative abs - Exist means existing Abs fuction Time for execution 1000000 iterations using interpreted Euporia 4.01 Integer - 1 element: New, Exist, New/Exist ratio{0.047, 0.017, 2.764705882} Double - 1 element: New, Exist, New/Exist ratio{0.094, 0.032, 2.9375} Integer - 1000 elements: New, Exist, New/Exist ratio{23.86, 16.735, 1.425754407} Double - 1000 elements: New, Exist, New/Exist ratio{48.954, 38.548, 1.269949154}
.. .. .. .. .. .. .. .. .. ..
-- Method B compiled New means Alternative abs - Exist means existing Abs fuction Time for execution 1000000 iterations using exe file created under Euporia 4.01 Bind Integer - 1 element: New, Exist, New/Exist ratio{0.047, 0.031, 1.516129032} Double - 1 element: New, Exist, New/Exist ratio{0.11, 0.062, 1.774193548} Integer - 1000 elements: New, Exist, New/Exist ratio{23.906, 16.812, 1.421960504} Double - 1000 elements: New, Exist, New/Exist ratio{49.047, 38.516, 1.273418839}
.. .. .. .. .. .. .. .. .. ..
While there are some discrepancies in the above numbers, I tried several times and the results are typical of what I got.