I was trying to benchmark a program I have writing against Mathematica (or better said the internal Kernels used, like MKL).

Since my program is not threaded I wanted to compare with Mathematica 10 running in a single core (I have a i7 processor). From this I discovered a certain option that seems to control the number of threads used by Mathematica and to my surprise, if I use it, I get better performance from Mathematica if I force it to use one threads.

In[1] := SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> 4];
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> 4];
n = 1024;
t = Table[RandomReal[], {i, 0, n}, {j, 0, n}];
Timing[Inverse[t]] // First

Out[1] := 0.626792

The above is the default and it takes 0.62 seconds to invert the matrix. If I change the option I get better performance:

In[2] := SetSystemOptions["ParallelOptions" -> "ParallelThreadNumber" -> 1];
SetSystemOptions["ParallelOptions" -> "MKLThreadNumber" -> 1];
n = 1024;
t = Table[RandomReal[], {i, 0, n}, {j, 0, n}];
Timing[Inverse[t]] // First

Out[2] := 0.203677

I find hard to believe I found a bug in Mathematica, most likely I don't understand how this options work. Can somebody tell me if I am using these options correctly? Does the second line contain the correct code to force Mathematica to use only one thread?

  • $\begingroup$ Ok, I found part of the answer. It seems that the AbsoluteTiming is the right way to measure wall time for functions. Still, I would like to see if this is the correct way to control the number of threads. $\endgroup$
    – alfC
    Jul 3, 2016 at 7:39
  • 3
    $\begingroup$ These things are not documented, but you can always check how many cores are being used with your process manager. So we can't really say if it's the right way. But I can say that I would do the same. Timing measures (time spent by core 1) + (time spent by core 2) + (time spent by core 3) + (time spent by core 4). AbsoluteTiming measures the physical time elapsed. $\endgroup$
    – Szabolcs
    Jul 3, 2016 at 7:45
  • $\begingroup$ @Szabolcs, thanks. BTW, using AbsoluteTiming the results are 0.134 and 0.2160 respectively. $\endgroup$
    – alfC
    Jul 3, 2016 at 8:01


Your Answer

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

Browse other questions tagged or ask your own question.