Curious timing of 3 {Re,Im} extractors
- To: mathgroup at smc.vnet.net
- Subject: [mg42140] Curious timing of 3 {Re,Im} extractors
- From: carlos at colorado.edu (Carlos Felippa)
- Date: Fri, 20 Jun 2003 04:57:37 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
I timed 3 forms that process expr1 + expr2*I forms, where
expr1 and expr2 are real, to extract the Re and Im parts.
(They should not be used with complex fractions).
Times are with 4.2 on a Mac G4/867.
ComplexExpandReIm[z_]:=Module[{iunit,zz},
zz=ComplexExpand[z]/.I->iunit;
Return[{zz/.iunit->0,Coefficient[zz,iunit]}]];
ComplexExpandReIm1[z_]:=ComplexExpand[{Re[z],Im[z]}];
ComplexExpandReIm2[z_]:=ComplexExpand[{(z+Conjugate[z])/2,
(z-Conjugate[z])/(2*I)}];
n=200; k=50; z=(a+b*I)^k;
t1=Timing[Do[{x,y}=ComplexExpandReIm [z],{n}]]; Print[t1];
t2=Timing[Do[{x,y}=ComplexExpandReIm1[z],{n}]]; Print[t2];
t3=Timing[Do[{x,y}=ComplexExpandReIm2[z],{n}]]; Print[t3];
{1.53333 Second, Null}
{2.48333 Second, Null}
{5.81667 Second, Null}
The ratio (very roughly 1:2:4) has been fairly consistent over
sufficiently complicated expressions. I expected the first
function, which is contrived, to be the slowest one.
Why is it the fastest?