       Re: Why overloaded arithmetic operations are so slow?

• To: mathgroup at smc.vnet.net
• Subject: [mg49914] Re: [mg49729] Why overloaded arithmetic operations are so slow?
• From: "Fred Simons" <f.h.simons at tue.nl>
• Date: Fri, 6 Aug 2004 03:09:29 -0400 (EDT)
• References: <200407291145.HAA10328@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```Dear Sergey,

To explain what is going on I define a function that will produce a list of
zz-numbers:

In:=
p[n_] :=  zz /@ Table[Random[Integer, {1,1000}], {n}]

and adapt the assignment to zz to visualize the evaluation process:

In:=
Clear[zz];
zz/:Plus[z1_zz,z2_zz]:=(Print[{z1, z2}];zz[First[z1]+First[z2]])

The function Plus has attributes OneIdentity and Flat and these will be used
before the rule for zz is applied. That means that the arguments of Plus
will be sorted. The definition for zz uses only two arguments, so the
attribute Flat will be used to 'rewrite' the expression in such a way that
the rule for zz can be applied. Here is a small example.

In:=
p
Plus @@%

Out=
{zz,zz,zz,zz}
From In:=
{zz,zz}
From In:=
{zz,zz}
From In:=
{zz,zz}
Out=
zz

Indeed, the arguments are sorted, brackets are placed around the first two
arguments and the rule is applied, the arguments are sorted, brackets are
placed around the first two arguments and the rule is applied and so on. But
when we look at a larger example, we observe that the pattern matcher does
not simply place brackets around the first two arguments. It seems to
consider practically all possibilities before brackets are placed. In the
following example we observe that nearly all the time is used for the
transition from 15 to 14 arguments.

In:=
p
Plus @@%

Hence in this example the patternmatcher could be improved. But actually the
problem is due to a not quite adequate definition for zz. What you really
want to do is repeatedly combining two arguments of Plus leaving the others
untouched. That can be done in the following way:

In:=
Clear[zz];
zz/:Plus[z1_zz,z2_zz, z3___]:=Plus[zz[First[z1]+First[z2]], z3]

In:=
Total[p] // Timing
Out=
{0.032 Second,zz}

Or without the recursion:

In:=
Clear[zz];
zz/:Plus[z1__zz]:=zz[Plus @@ First /@ {z1}]

In:=
test=p;
(Total[test]) // Timing
Out=
{0.266 Second,zz}

I hoppe this answers your question. But if this example really is your
problem, it can be done much easier and faster without the zz-rule:

In:=
test = p;
Total[test]// Timing
zz[Fold[#1+#2[]&, 0, test]] // Timing
zz[Total[First/@test]] // Timing
Out=
{2.75 Second,zz}
Out=
{0.047 Second,zz}
Out=
{0.015 Second,zz}

Kind regards,

Fred Simons
Eindhoven University of Technology

----- Original Message -----
From: "Sergey Afonin" <serg at msu.ru>
To: mathgroup at smc.vnet.net
Subject: [mg49914] [mg49729] Why overloaded arithmetic operations are so slow?

> Hi all.
>
> standard arithmetic operations, such as Plus, for user-defined data is
> "much more convenient" than defining specific functions. But how this
> can be done efficiently? I want to define type of the form zz[Number]
>
> zz /: Plus[z1_zz, z2_zz] := zz[First[z1] + First[z2]]
>
> In fact, zz are "normal" numbers. The following statement prints out
> the time spent for addition wrt the number of elements...
>
> Do[Print[Timing[Plus @@ Table[zz[10^(i - 1)], {i, n}]]], {n, 15}]
>
>
> {0. Second, zz}
> {0. Second, zz}
> {0. Second, zz}
> {0. Second, zz}
> {0. Second, zz}
> {0. Second, zz}
> {0. Second, zz}
> {0.02 Second, zz}
> {0.03 Second, zz}
> {0.12 Second, zz}
> {0.35 Second, zz}
> {1.07 Second, zz}
> {3.31 Second, zz}
> {10.07 Second, zz}
> {30.81 Second, zz}
>
> Is it normal?
>
> Best regards,
> Sergey Afonin
>

```

• Prev by Date: Re: populate a list with random numbers from normal distribution?
• Next by Date: Mathematica on Linux - problems compiling for Netsolve
• Previous by thread: Continuous Distributions
• Next by thread: Mathematica on Linux - problems compiling for Netsolve