The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Inactive » comp.soft-sys.math.mathematica

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: Timing puzzle
Replies: 3   Last Post: Apr 12, 2013 2:15 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Bob Hanlon

Posts: 906
Registered: 10/29/11
Re: Timing puzzle
Posted: Apr 11, 2013 4:10 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

I've added a couple of additional (faster) methods. You did not indicate
the value of n that you used. These timings are with Mathematica v9.0.1.0
on a MacBook Air (2.13 GHz Intel Core 2 Duo; OS X 10.8.3) and n = 10^4.

poly initialization (when required) moved inside Timing to level the
playing field.

Removed unnecessary Print statements.

n = 10^4;

p[arg_] := {
mycolor[Random[], Random[], Random[]]],
{Random[], Random[]},
{Random[], Random[]},
{Random[], Random[]}}]]};

Timing[poly = {}; Do[
AppendTo[poly, p[i]], {i, n}]][[1]]

Timing[poly = {}; Do[
poly = Append[poly, p[i]], {i, n}]][[1]]

Timing[poly = {}; Do[
poly = Join[poly, {p[i]}], {i, n}]][[1]]

Timing[poly = {}; Do[
poly = {poly, {p[i]}}, {i, n}];
poly = Flatten[poly]][[1]]

Timing[poly = Table[0, {n}];
Do[poly[[i]] = p[i], {i, n}]][[1]]

poly = Table[p[i], {i, n}]][[1]]

poly = p /@ Range[n]][[1]]


Bob Hanlon

On Wed, Apr 10, 2013 at 12:53 AM, <>wrote:

> I am writing a graphics package that often creates objects with thousands
> of polygons, possibly up to 10^5. Out of curiosity I tested 5 ways of
> dynamically creating a plot list, using AppendTo, Append, Join, etc., and
> did the following timing test of 5 ways to do it:
> ClearAll[poly,p,n]; poly={}; n00;
> p[arg_]:= {mygraphic[mycolor[Random[],Random[],Random[]]],
> mygraphic[mypoly[{{Random[],Random[]},
> {Random[],Random[]},{Random[],Random[]}}]]};
> Print[Timing[Do[AppendTo[poly,p[i]],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Append[poly,p[i]],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly=Join[poly,{p[i]}],{i,1,n}]][[1]]];
> ClearAll[poly]; poly={};
> Print[Timing[Do[poly={poly,{p[i]}},{i,1,n}];poly=Flatten[poly]][[1]]];
> ClearAll[poly]; poly=Table[0,{n}];
> Print[Timing[Do[poly[[i]]=p[i],{i,1,n}]][[1]]];
> Running with n00 on a MacPro under Mac OSX 10.6.8 gives these times:
> 0.911327 Second
> 0.891656 Second
> 0.927267 Second
> 0.504454 Second
> 0.009575 Second
> Question: why is the last method much faster? I thought that appending an
> object to a list should take about the same time as storing an array entry.
> When I worked with linked lists several decades ago (using assembly code
> on a CDC 7600) all I had to do is retrieve the object address, manipulate
> registers, store in a pointer array, and presto! it was done.

Date Subject Author
Read Re: Timing puzzle
Bob Hanlon
Read Re: Timing puzzle
Sseziwa Mukasa

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.