Ralf Hemmecke wrote:
>
> On 03/01/2017 02:50 PM, Waldek Hebisch wrote:
>
> > So there are reasons to have 'zero?' and 'one?' but naturaly their
> > use should be limited. Concerning efficiency, using 'not(a = b)'
> > frequently gives speed gain compared to 'a ~= b'.
>
> That surprises me. If there is a domain that has this property then why
> not rewriting its implemention of ~= to
>
> a~=b == not(a=b)
>
> ? This could at most be one additional (indirect) function call (which
> might even be inlined away).
> Do you have an example, where this "problem" occurs?
The performance difference is exactly due to indirect function
calls. 'not' is inlined and after inlininig is essentialy one
machine instruction. '=' may be hairy, but there are lot of cases
where it can be inlined. For simple types '=' may be further
optimized by Lisp compiler, in important case of array indices to
one machine instruction. So instead of 2 indirect calls (call to '~=',
and call to '=' calling general Lisp '=' (which dynamically checks
argument types) we get one or 2 machine instructions. So
when dealing with say small integers we may get speedup of order
50-100 for comparison which easily affects run time of larger
piece of code. Comparatively, for integers 'n = 0' due to
inlining is going to produce essentially optimal code.
On more hairy types there is need to actually compute '0' and
call '='. In such case 'zero?' may save cost of computing '0',
one less argument to pass and possibly some shortcuts when
actually performing comparison. Cost of computing 0 is relatively
small because '0' is normally declard constant and cached,
so after first use that is just cost of function call.
Comparably to other saving one argument is probaby negligible.
Inside 'zero?' we freqently do call to '=' in which case there
is no saving. In case of free modules representation is
a list and check for zero is checking for empty list.
This is quite fast, but general case is not much worse:
it iterates over two lists and since one list is empty
loop performs zero iterations. Anyway, for free modules
main cost is function calls and 'zero?' may reduce number
of calls from 2 to 1. So comparison using 'zero?' may be
twice as fast as 'p = 0'. But in grand picture this is
not likely to matter much: typical operations in free
modules perform much more operations and saving due to
'zero?' is unlikely to matter.
--
Waldek Hebisch
--
You received this message because you are subscribed to the Google Groups
"FriCAS - computer algebra system" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/fricas-devel.
For more options, visit https://groups.google.com/d/optout.