Re: Speeding up code

• To: mathgroup at smc.vnet.net
• Subject: [mg100432] Re: Speeding up code
• From: dh <dh at metrohm.com>
• Date: Wed, 3 Jun 2009 05:28:26 -0400 (EDT)
• References: <h02vo6\$8ek\$1@smc.vnet.net>

```
Hi Alexander,

see below, Daniel

borisov at sas.upenn.edu wrote:

> Hello all,

>

>

> I need some advise on speeding up code. Here is a section that is executed more

> than 100000 times during my simulations. Currently it takes about 0.06s per

> execution which is about 80% of the total time for the simulation. The

> subsection MITime itself takes 0.03s out of those 0.06s. All variables not

> defined in this section are predefined. Anyone have suggestions?

>

> MDTime = First[

>   Timing[

>    yps = Table[(ys[[i]] + ys[[i - 1]])/2, {i, 2, ngrid}];

this can be written: yps= Differences[ys]/2

>    yms = Table[(ys[[i]] - ys[[i - 1]]), {i, 2, ngrid}];

>    ups = Table[(us[[i]] + us[[i - 1]])/2, {i, 2, ngrid}];

>    ums = Table[(us[[i]] - us[[i - 1]]), {i, 2, ngrid}];

>    Etus = Table[rrms[[i]]*Exp[-ups[[i]]], {i, 1, ngrid - 1}];

>    (*Etu2s=Table[Exp[-ups[[i]]/2],{i,1,ngrid-1}];*)

>

>    SqEts = Table[

>      rrpms[[i]]*sqrrs[[i]]*Exp[-ups[[i]]/2]*acubed*rba, {i, 1,

>       ngrid - 1}];

this can be written: SqEts= rrpms sqrrs Exp[-ups/2] acubed rba

>    ElemEIEI = Table[N[SqEts[[i]]/4, Precis], {i, ngrid - 1}];

>    ElemOIOI = Table[N[-Etus[[i]]/2, Precis], {i, ngrid - 1}];

>    ElemOIOI1 =

>     Table[N[yps[[i]]*Etus[[i]]/2, Precis], {i, ngrid - 1}];

>    Es[[ngrid*nvar]] = N[Exp[us[[ngrid]]] - rrs[[ngrid]], Precis];

>    Es[[1]] = Exp[us[[1]]] - rrs[[1]]*ys[[1]];

>    For[i = 1, i <= ngrid - 1, i++,

>     Es[[nvar*i]] = N[yms[[i]] + Yds[[i]] - SqEts[[i]], Precis];

>     Es[[nvar*i + 1]] = N[ums[[i]] - yps[[i]]*Etus[[i]], Precis];

>     ];

the loop can be written:

Es[[nvar Range[ngrid-1]]]= N[yms + Yds - SqEts, Precis]

Es[[nvar Range[ngrid-1]+1]]= N[ums - yps Etus, Precis]

>    (* Table of rules used to create the Sms matrix*)

>

>    MITime = First[

>      Timing[

>       DataSAP = Join[

>         Table[{nvar*i, nvar*(i - 1) + 1} -> N[-1., Precis], {i,

>           ngrid - 1}],

this can be written:

Thread[Transpose[{nvar*Range[ngrid-1], nvar*(Range[ngrid-1] - 1) + 1}]

-> N[-1., Precis]]

>         Table[{nvar*i, nvar*(i - 1) + 2} -> ElemEIEI[[i]], {i,

>           ngrid - 1}],

this can be written:

Thread[Transpose[{nvar*Range[ngrid-1], nvar*(Range[ngrid-1] - 1) + 1}]

-> ElemEIEI]

>         Table[{nvar*i, nvar*(i - 1) + 3} -> N[1., Precis], {i,

>           ngrid - 1}],

>         Table[{nvar*i, nvar*(i - 1) + 4} -> ElemEIEI[[i]], {i,

>           ngrid - 1}],

>         Table[{nvar*i + 1, nvar*(i - 1) + 1} -> ElemOIOI[[i]], {i,

>           ngrid - 1}],

>         Table[{nvar*i + 1, nvar*(i - 1) + 2} ->

>           ElemOIOI1[[i]] - 1., {i, ngrid - 1}],

>         Table[{nvar*i + 1, nvar*(i - 1) + 3} -> ElemOIOI[[i]], {i,

>           ngrid - 1}],

>         Table[{nvar*i + 1, nvar*(i - 1) + 4} ->

>           ElemOIOI1[[i]] + 1., {i, ngrid - 1}]]

>       ]

>      ];

>    DataSA = Prepend[DataSAP, {1, 2} -> N[Exp[us[[1]]], Precis]];

>    DataSA =

>     Prepend[DataSA, {nvg, nvg} -> N[Exp[us[[ngrid]]], Precis]];

>    DataSA = Prepend[DataSA, {1, 1} -> N[-rrs[[1]], Precis]];

>    Sms = SparseArray[DataSA];

>   ]

> ];

>

>

>

> Best regards

> Alexander

>

```

• Prev by Date: Re: Speeding up code
• Next by Date: two questions
• Previous by thread: Re: Speeding up code
• Next by thread: Re: Speeding up code