Re: Polygon cutter
- To: mathgroup at smc.vnet.net
- Subject: [mg87982] Re: Polygon cutter
- From: carlos at Colorado.EDU
- Date: Mon, 21 Apr 2008 06:40:28 -0400 (EDT)
- References: <200804200349.XAA11201@smc.vnet.net> <fuhf9s$ib1$1@smc.vnet.net>
On Apr 21, 1:21 am, "W_Craig Carter" <ccar... at mit.edu> wrote: > Hello Carlos, > I believe the interpretation of good style is a bit like ethics--it's > easy to agree in extreme cases, but the in-between cases are > subjective. > > I think this depends on whether you intend to modify the code in the > future. I believe V1 is easier to read, and if you think that someone > else may wish to modify it, then I recommend sticking with that. > However,"Which" will probably be more efficient, and rank the order of > tests with decreasing frequency. > > V2 looks cool--some people like that. There is probably a way to > improve the coolness even more, and remove the need to return the > impossible "Null"s. Creating V2 probably hones your skills and is > more interesting to do, which is similarly important; but probably has > a direct impact on a broader interpretation of efficiency. > > On Sat, Apr 19, 2008 at 11:49 PM, <car... at colorado.edu> wrote: > > A programming style question. The code fragments below come > > Version 1. Straight translation from Fortran: > > > p={Null}; > > If [tb>0&&tb==tt, p={P1,P2,P4,P3}]; > > : > : > > > > > poly=Graphics[Polygon[p]]; > > > Version 2. Table driven variant of above: > > > p={{{},{Pc1,Pc2,Pc3},{Pc1,P4,P3},{Pc1,Pc2,P3,P4}}, > > {{Null},{},{Null},{Null}}, > > {{Null},{Pc3,Pc2,P1,P2},{P1,P2,P4,P3},{Pc2,P1,P2,P4,P3}}, > > {{Null},{Pc3,P1,P2},{Null},{P1,P2,P4,P3}}} [[tb+2,tt+2]]; > > poly=Graphics[Polygon[p]]; > > > Question: which version is preferable in Mathematica, or is > > there a better one? Both run roughly at the same speed > > (about 25 microsec under 5.2 on an Intel MacBook Pro). > > Since this loop is traversed once for each polygon, > > efficiency is important. > > -- > W. Craig Carter Your suggestion of looking at the Which construct was an interesting one - didn't know it existed. So I tried it as p={Null}; Which [tb>0&&tb==tt, p={P1,P2,P4,P3}, tb==1&&tt==2, p={Pc2,P1,P2,P4,P3}, tb==-1&&tt==-1,p={}, tb==-1&&tt==0= , p={Pc1,Pc2,Pc3}, tb==-1&&tt==1, p={Pc1,P4,P3}, tb==-1&&tt==2, p={Pc1,Pc2,P3,P4}, tb==0&&tt==0, p={}, tb==1&&tt==0, p={Pc3,Pc2,P1,P2}, tb==2&&tt==0, p={Pc3,P1,P2}]; poly=Graphics[Polygon[p]]; The two most common cases are the first two. I expected this to be quicker than V1 but it wasnt. On a MacBook Pro under 5.2: V1 25 microsec/polygon V2 24 Which 30 Similar rankings for 4.1 and 4.2. Havent tested it on 6.0.
- References:
- Polygon cutter
- From: carlos@Colorado.EDU
- Polygon cutter