Speeding up code
- To: mathgroup at smc.vnet.net
- Subject: [mg100392] Speeding up code
- From: borisov at sas.upenn.edu
- Date: Tue, 2 Jun 2009 06:46:51 -0400 (EDT)
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}]; 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}]; 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]; ]; (* 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}], Table[{nvar*i, nvar*(i - 1) + 2} -> ElemEIEI[[i]], {i, ngrid - 1}], 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