4
$\begingroup$

I've noticed the following significant different in performance for different formulations of the same function:

Exp[-t - 256 I Pi t] == Exp[-t] Exp[-256 I Pi t]
Table[Exp[-t - 256 I Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
Table[Exp[-t] Exp[-256 I Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* True, 0.765, 0.496 *)

I've got a function with the form below, which oddly splits the difference

func[a_, b_] := Exp[-a t] Exp[-b I Pi t]
func[1, 256] == Exp[-t - 256 I Pi t]
Table[func[1, 256], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* True, 0.614 *)

The way Mathematica is storing the complex exponential seems to be the slower one:

Exp[-t - 256 I Pi t]
Table[%, {t, 0, 1, 1/30000}]; // AbsoluteTiming
Exp[-t] Exp[-256 I Pi t]
Table[%, {t, 0, 1, 1/30000}]; // AbsoluteTiming

enter image description here

The issue does appear to be caused by the presence of complex values

Exp[-t - 256  Pi t] == Exp[-t] Exp[-256  Pi t]
Table[Exp[-t - 256  Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
Table[Exp[-t] Exp[-256  Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* True, 0.37, 0.37 *)

I can reproduce this behavior in v9 and v10 on Win 7/64-bit. I am looking for insight into how one might achieve the speeds observed when the exponential terms are not combined.

$\endgroup$
3
$\begingroup$

Sorry, this is too long for a comment, but I thought it might help!


Update

I also tried, interestingly,

c = Table[Exp[-t (1 + 256 I Pi )], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* 0.109373 seconds *)

d = Table[Exp[t (-1 - 256 I Pi )], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* 0.140624 seconds *)

Though this method in fact produces a slightly different list than the options below. For example,

a - c
(* {0, E^(-(1/30000) - (16 I \[Pi])/1875) - E^((-1 - 256 I \[Pi])/30000), ..., 0} *)

N[a] - N[c]
(* {0., 0. + 0. I, 0. + 0. I, ... , 0.} *)

Chop[N[a] - N[c]]
(* {0, 0, 0, 0, ..., 0} *)

So one has to do a bit of manipulation to get the same answer!


Original comment

I tried the following.

a = Table[Exp[-t - 256 I Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
b = Table[Exp[-t] Exp[-256 I Pi t], {t, 0, 1, 1/30000}]; // AbsoluteTiming
(* 0.390862 seconds *)
(* 0.250003 seconds *)

a == b
(* True *)

I then put Mathematica in Debug mode as per Profiling from Mathematica and ran this:

Table[Exp[-t - 256 I Pi t], {t, 0, 1, 1/30000}]; // RuntimeTools`Profile
Table[Exp[-t] Exp[-256 I Pi t], {t, 0, 1, 1/30000}]; // RuntimeTools`Profile

The following images are the output, and might help with your question:

I am looking for insight into how one might achieve the speeds observed when the exponential terms are not combined.

enter image description here

Finally, if you then perform the same test on the function in my update posted above, the result is thus:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.