3

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.

cj9435042
  • 683
  • 3
  • 6
  • 4
    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. – Michael E2 May 08 '18 at 01:35
  • 7
    I'd urge you to not use N as a variable name. That is the name of a predefined function http://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 – Bill May 08 '18 at 02:14

0 Answers0