Re: Timing For loops!
- To: mathgroup at smc.vnet.net
- Subject: [mg60154] Re: [mg60111] Timing For loops!
- From: Quang-Tuong.Nguyen at lpn.cnrs.fr
- Date: Sun, 4 Sep 2005 05:07:23 -0400 (EDT)
- References: <200509020833.EAA05924@smc.vnet.net> <200509030605.CAA19035@smc.vnet.net> <20050903224924.GG30053@wolfram.com>
- Sender: owner-wri-mathgroup at wolfram.com
Thank for your answer. anh sorry for the code, i made an error when i
posted the message.
I give here the code and my problem.
pmax=22 ;
nbdenormek=30 ;
nbdetheta=70 ;
pastheta=Pi/36 ;
pasnormedek=0.05/30 ;
(* band[m], vecta[m] are function that defined above*)
HBFpot[m_,l_] :=Module[{bm=band[m],bl=band[l],vl=vectq[l],vm=vectq[m]},
If[bm==bl,Vtot[[bm,2pmax+1+(vm-vl)]],0]+coefHBF*Sum[Exp[I*(-vm+vl)*sopi*poscouche[[co+1]]]*couplageHbf[[co]],{co,1,nbcouches-1}]*
*couplageHBFsousmat[[bm,bl]]]] ;
For[indicenormedek=1,indicenormedek<=nbdenormek,indicenormedek++,
For[indicetheta=1,indicetheta<=nbdetheta,indicetheta++,
normedek=normedekmin+(indicenormedek-1)*pasnormedek;
theta=thetainit+(indicetheta-1)*pastheta;
Print[¡§ Using For loops to store the result of calculation, where
TFHbulktotal[kx_,ky_,ky_q_] is a function that is defined above. Here I
want to store the calculation of TFHbulktotal in Hkptotal[p,q] because
I¡¦ll use Hkptotal[p,q] in the next iteration ¡§];
For[p=-pmax,p?Tpmax,p++,
For[q=-pmax,q?Tpmax,q++,
Hkptotal[p,q]=TFHbulktotal[kmoins[normedek*Cos[theta],normedek*Sin[theta]],kplus[normedek*Cos[theta],normedek*Sin[theta]],p*sopi,(-p+q)*sopi]
]];
Hkphetero[m_,l_]:=Hkptotal[vectq[m],vectq[l]][[band[m],band[l]]];
Hmatkp=Table[Hkphetero[m,l],{m,1,indmax},{l,1,indmax}];
Hmat=Hmatkp+HBFpotmat; (* here HBFpotmat is calculated easily above*)
sol =Eigensystem[Hmat];
Blochmat=sol[[2]];
Energy=Re[sol[[1]]];
listcond=Sort[Transpose[Select[Transpose[{Range[Length[Energy]],Energy}],(#[[2]]>BCmin)&]][[1]],Greater];
listval=Sort[Transpose[Select[Transpose[{Range[Length[Energy]],Energy}],(#[[2]]<BVmax)&]][[1]],Greater];
enecond[[indicenormedek,indicetheta,All]]=Sort[Select[Energy,(#>BCmin)&]];
eneval[[indicenormedek,indicetheta,All]]=Sort[Select[Energy,(#<BVmax)&],Greater];
]
];
> and thus spake NGUYEN Quang Tuong [2005.09.03 @ 01:31]:
>> Hi,
>>
>> I wrote a program in which i used the loop for to store the evaluation
>> of an expression ( in order to reduce the time of calculation ). But my
>> program is until slow. I would like to kwow if we can have another to
>> reduce the time of calculation.
>
> In general, For[], Do[], and While[] loops are slower than their
> functional equivalents in Mathematica. You can probably do what you
> want using Map[] and Table[]. Unfortunately the code you gave below
> is incomplete and makes no sense, so it's hard to say for sure. Try
> posting some more specific example code. Thanks!
>
>>Here in the loop for that i used.
>>
>> pmax=10;
>>
>>
>> Here TFHbulktotal is a function that is defined previously. And I'll
>> use Hkptotal[p,q] for the next calculation..
>>
>>
>> nbdenormek=30;
>> nbdetheta=70;
>> normedekmin=0;
>>
>>
>>
>>
>> What I must do to replace the two loop For above to reduce the time of
>> calculation ??
>>
>> Thanks a lot!
>>
>
> --
> /*------------------------------*\
> | stephen layland |
> | Documentation Programmer |
> | http://members.wri.com/layland |
> \*------------------------------*/
>
--
NGUYEN QUANG Tuong
PhD student
Laboratory for Photonics and Nanostructures
LPN,UPR20, CNRS. Route de Nozay, 91460 Marcoussis, France.
Tel : (33)+1 69 63 60 43
Fax : (33)+1 69 63 60 06
Home : Ch 68, Maison du Japon
Cite Internationale, 7 Boulevard Jordan.
75014 Paris, France
Tel : (33)+1 44 16 12 28
- References:
- DSolve problem
- From: "sunnewton" <sunshinenewton@gmail.com>
- Timing loop For !!
- From: NGUYEN Quang Tuong <quang-tuong.nguyen@lpn.cnrs.fr>
- DSolve problem