My code requires to set some precision to get more accurate results. But I found when I set the precision like 20 or 30, the computation speed would jump 7-10 times slower...

Here's an example:

Block[{W = 64, d, Theta,MeshTable }, d = 2.0/W; 
     MeshTable =Table[{i, j}, {j, 1.0 - d/2, -1.0, -d}, {i, -1.0 + d/2, 1.0,d}];
  Theta = Apply[ArcTan, MeshTable, {2}];
Table[E^(-I*q*Theta), {p, 0, 20}, {q, -p, p, 2}];] // AbsoluteTiming // First

The timing result running on my PC is 0.454783 sec.

While if I use SetPrecision:

 Block[{W = 64, d, Theta}, d = 2.0/W;
  MeshTable = Table[{i, j}, {j, 1.0 - d/2, -1.0, -d}, {i, -1.0 + d/2, 1.0, d}];
  Theta = SetPrecision[Apply[ArcTan, MeshTable, {2}], 30];
  Table[E^(-I*q*Theta), {p, 0, 20}, {q, -p, p, 2}];] // AbsoluteTiming // First

The timing would cost 3.753 sec or so.

The fact is when dealing with large numbers, the time difference is so unacceptable. I tried to use method in this thread to set the global precision instead, but still not work.

I really hope to keep the speed as best as it could after the precision set.

  • 4
    $\begingroup$ There are several kinds of numbers, exact (including machine-size integers), machine-precision floating-point numbers, and arbitrary-precision numbers. Computing with machine numbers is faster because Mathematica uses native CPU operations. Your use of SetPrecision makes Mathematica use arbitrary-precision numbers, which use efficient but slower software routines for computing. You have to choose between machine vs. arbitrary precision. $\endgroup$
    – Michael E2
    Commented May 8, 2018 at 1:35
  • 7
    $\begingroup$ I'd urge you to not use N as a variable name. That is the name of a predefined function reference.wolfram.com/language/ref/N.html and Mathematica can behave a little oddly if you start redefining names. Sometimes you can get away with it, but that is a very bad habit to get into $\endgroup$
    – Bill
    Commented May 8, 2018 at 2:14


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.