       RE: MatrixTemplate (was SpecialMatrix)

• To: mathgroup at smc.vnet.net
• Subject: [mg28451] RE: [mg28414] MatrixTemplate (was SpecialMatrix)
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.de>
• Date: Thu, 19 Apr 2001 03:26:39 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Dear Allan, dear Carl,

these are most miraculous discoveries you both made.
The Question arises, what makes CreateMatrix such fast?
and what did you really get? a fully laid-out matrix?
This is not so, I believe. For my investigation

> -----Original Message-----
> From: Allan Hayes [mailto:hay at haystack.demon.co.uk]
To: mathgroup at smc.vnet.net
> Sent: Tuesday, April 17, 2001 5:54 AM
> To: mathgroup at smc.vnet.net
> Subject: [mg28451] [mg28414] MatrixTemplate (was SpecialMatrix)
>
>
> The problem of creating a matrix with all the entries the
> same, say all
> zero, has cropped up in recent postings.
> The following seems to be very quick way of doing this:
>
> Clear["`*"]
> CreateMatrix[x_,m_,n_]:=
>
> Check
>
> CreateMatrix[0,3,5]
>
>         {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
>
> Timings
>
> CreateMatrix[0,5000,5000];//Timing
> CreateMatrix[0,5000,2500];//Timing
> CreateMatrix[0,2500,5000];//Timing
> CreateMatrix[0,5000,250];//Timing
> CreateMatrix[0,250,5000];//Timing
>
>         {0. Second,Null}
>
>         {0.11 Second,Null}
>
>         {0. Second,Null}
>
>         {0.06 Second,Null}
>
>         {0.16 Second,Null}
>
> For comparison:
>
> IdentityMatrix;//Timing
>
>         {10.27 Second,Null}
>
> and. using Table,
>
> Clear["`*"]
> CreateMatrix[x_,m_,n_]:=
>   Table[Evaluate[Table[x,{n}]],{m}]
>
> CreateMatrix[0,3,5]
>
>         {{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}}
>
> CreateMatrix[0,5000,5000];//Timing
> CreateMatrix[0,5000,2500];//Timing
> CreateMatrix[0,2500,5000];//Timing
> CreateMatrix[0,5000,250];//Timing
> CreateMatrix[0,250,5000];//Timing
>
>         {10.22 Second,Null}
>
>         {5. Second,Null}
>
>         {4.67 Second,Null}
>
>         {0.44 Second,Null}
>
>         {0.55 Second,Null}
>
> --
> Allan
> ---------------------
> Allan Hayes
> Mathematica Training and Consulting
> Leicester UK
> www.haystack.demon.co.uk
> hay at haystack.demon.co.uk
> Voice: +44 (0)116 271 4198
> Fax: +44 (0)870 164 0565
>
>
>
>
[Hartmut Wolf]

In:= \$Version
Out= "4.0 for Microsoft Windows (April 21, 1999)"
In:= mem0 = MemoryInUse[]
Out= 1129872
In:= k0 = 80554 K ;

at my computer (Windows 2000).

Now we go through a sequence of steps, which are obvious:

In:=
Out= {0.01 Second, Null}
In:= mem1 = MemoryInUse[]
Out= 1158200
In:= k1 = 80532 K;

In:=
m2 = PadRight[{{}}, {2000, 3000}, 0]; // Timing
Out= {1.752 Second, Null}
In:= mem2 = MemoryInUse[]
Out= 25217776
In:= k2 = 104100 K;

In:=
m3 = Table[0, {2000}, {3000}]; // Timing
Out= {2.965 Second, Null}
In:= mem3 = MemoryInUse[]
Out= 49221952
In:= k3 = 127560 K;

...just for comparison, a Matrix of same size:

In:=
m4 = Partition[Range[2000*3000], 3000]; // Timing
Out= {0.17 Second, Null}
In:= mem4 = MemoryInUse[]
Out= 73225392
In:= k4 = 151024 K;

Now let's look at the memory used (per element):

In:=
Subtract @@@ Partition[{mem4, mem3, mem2, mem1, mem0}, 2, 1]/(2000.*3000.)
Out=
{4.00057, 4.0007, 4.00993, 0.00472133}

We see m2, m3, m4 use up four bytes/element each, which is what we
expect. However m1 is much more compact!

In:=
Subtract @@@ Partition[{k4, k3, k2, k1, k0}, 2, 1]
Out=
{23464 K, 23460 K, 23568 K, -22 K}

This is roughly consistent with recorded MemoryInUse.

In:= MemoryInUse[]
Out= 73231408
In:= kk0 = 151412 K;

Now we work with our "objects"

In:= m3 == m4 // Timing
Out= {0. Second, False}

It takes no time, to find out the discrepancy, ok.

In:= MemoryInUse[]
Out= 73234112
In:= kk1 = 151424 K;

...and the comparison takes no memory, ok.
However...

In:= m1 == m4 // Timing
Out= {4.797 Second, False}

... the comparison with m1 takes up more time, than was
gained in first place! Not ok!

In:= MemoryInUse[]
Out= 73236368
In:= kk2 = 275144 K;

...what happend here? Mathematica doesn't show additional
memory used, however much had been reclaimed from the OS!

In:= m2 == m4 // Timing
Out= {8.372 Second, False}
In:= MemoryInUse[]
Out= 73238624
In:= kk3 = 275144 K;

..also much time for m2 needed!

In:= m1 == m2 // Timing
Out= {0.521 Second, True}
In:= MemoryInUse[]
Out= 73240896

...this is fast

In:= kk4 = 275144 K;
In:= m2 == m3 // Timing
Out= {5.217 Second, True}
In:= MemoryInUse[]
Out= 73243144

In:= kk5 = 275544 K;
In:= m1 == m3 // Timing
Out= {4.887 Second, True}
In:= MemoryInUse[]
Out= 73245496
In:= kk6 = 275168 K;

I admit that these last timings are not fully conclusive,
and might be somewhat dependend on the structure of the hardware
used (This is a notebook computer from my company:
hp OmniBook 4150, and possibly there is a performance
step down at 128 MB due to chip set, furthermore there
could be some influence of paging for the last operations
(paging times are not recorded by Timing[] though),
my real memory was 192 MB).

I will refrain from fantasizing about what is going on,
whowever someone familiar with "Mathematica inside"
is asked to comment. Esp. to the question: what must be
paid for "rapid matrix creation", and when?

Kind regards, Hartmut

```

• Prev by Date: Re: Rearrangement of a sequence into random order
• Next by Date: AW: circular plot question
• Previous by thread: MatrixTemplate (was SpecialMatrix)
• Next by thread: Re: MatrixTemplate (was SpecialMatrix)