6
$\begingroup$

Here is a small section of code from this SE question which computes the real part of the dilogarithm (the upper limit of the sum is increased for demonstration purposes):

realRegion1 = Compile[{{x, _Real}}, Sum[x^k/k^2, {k, 1., 1000}]];

Here is the performance when compiled to the Wolfram Virtual Machine (WVM):

(* to WVM *)
Timing[Table[realRegion1[x], {x, -.9, .9, .001}];]

{0.260303, Null}

and the performance when compiled to C (which I get by running $CompilationTarget = "C" before running the realRegion1 code):

(*to C*)
Timing[Table[realRegion1[x], {x, -.9, .9, .001}];]

{0.096413, Null}

Now I run into a big problem when I pass the compiled function realRegion1[x] to Manipulate. As an example, try

Manipulate[Plot[realRegion1[a x], {x, -1, 1}, PlotRange -> {-1, 2.0}], {{a, 1}, .1, 1}]

If the function was complied to WVM, the controls are as slick as butter. But, if the function was compiled to C, they are very unresponsive. I am upset.

How do I understand/alleviate this sluggishness when compiled to C?

I am using Mathematica 9.0.1 on Mac OSX.

$\endgroup$
  • 3
    $\begingroup$ Very likely because you're passing symbolic arguments to the function (a x). Try Evaluated -> False in Plot. $\endgroup$ – Szabolcs Dec 17 '14 at 22:57
  • $\begingroup$ Quite interesting. The reason is exactly your special setting $CompilationTarget = "C", and the problem won't appear if CompilationTarget -> "C" is used! Don't why though……BTW, the solution from @Szabolcs also works. $\endgroup$ – xzczd Dec 18 '14 at 6:02
  • $\begingroup$ @Szabolcs Setting the option Evaluated->False does make it more responsive. I'm trying to find more info on this option, but the documentation doesn't seem to exist. Do you know where I can find more information? $\endgroup$ – QuantumDot Dec 18 '14 at 15:06
2
$\begingroup$

I would solve that problem adding a function with a pattern in the argument. Try:

realRegion1a[x_Real] := realRegion1[x]

And the try your Manipulate using that new function. It will behave as expected.

$\endgroup$
  • $\begingroup$ This seems not to work, at least on v8.0.4, vista 32bit. $\endgroup$ – xzczd Dec 18 '14 at 6:08
  • $\begingroup$ I don't have anymore M8, so cannot test there, but tested it in 9 and 10 and worked fine. $\endgroup$ – FJRA Dec 18 '14 at 16:11
  • $\begingroup$ This solution appears to work in 9.0.1 MacOSX and in 10.0.1 on Linux. $\endgroup$ – QuantumDot Dec 18 '14 at 19:12

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.