MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Why overloaded arithmetic operations are so slow?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg49729] Why overloaded arithmetic operations are so slow?
  • From: Sergey Afonin <serg at msu.ru>
  • Date: Thu, 29 Jul 2004 07:45:32 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hi all.

In "The Mathematica Book" (2.5.10) one can read that overloading 
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] 
with the addition

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[1]}
{0. Second, zz[11]}
{0. Second, zz[111]}
{0. Second, zz[1111]}
{0. Second, zz[11111]}
{0. Second, zz[111111]}
{0. Second, zz[1111111]}
{0.02 Second, zz[11111111]}
{0.03 Second, zz[111111111]}
{0.12 Second, zz[1111111111]}
{0.35 Second, zz[11111111111]}
{1.07 Second, zz[111111111111]}
{3.31 Second, zz[1111111111111]}
{10.07 Second, zz[11111111111111]}
{30.81 Second, zz[111111111111111]}

Is it normal?

Best regards,
Sergey Afonin


  • Prev by Date: PutAppend Command and Data Output
  • Next by Date: RE: Combinations
  • Previous by thread: Re: PutAppend Command and Data Output
  • Next by thread: Re: Why overloaded arithmetic operations are so slow?