0
$\begingroup$

I'm trying to compare the time that it takes to use the command LinearSolve when solving matrix(with random integers from 3 to 150 in each position) that are 10x10,20x20,30x30, ... , 150x150 for the first 7 I get " 0. " as the result. Is there any way for the command to tell me more exact values, with more decimals? Also, for the matrix of 80x80 and more I get the same result of 0.015625. Is this normal for the command to take the same time to solve these different matrix?

$\endgroup$
  • 3
    $\begingroup$ Note that timings measured with Timing also involve time spent in the frontend. It is more reasonable to use AbsoluteTiming. You should use RepeatedTiming for more accurate measurements: It runs the code several times and returns the average runtime. This way, some artifacts can be averaged out. Note also that every timing below, say a millisecond, should not be taken too seriously. $\endgroup$ – Henrik Schumacher May 6 '18 at 15:11
3
$\begingroup$

So, to properly investigate this let's write a quick benchmarking function, like:

test[n_, tf_] := Block[{m, b},
   m = RandomInteger[{3, 150}, {n, n}];
   b = RandomInteger[{3, 150}, n];
   First@tf[LinearSolve[m, b]]];

test takes an argument that determines the matrix size (n) and the timing function tf. Timing, AbsoluteTiming, and RepeatedTiming are the most commonly used. On my machine:

Table[test[n, Timing], {n, 10, 150, 10}]

{0., 0., 0., 0.0156001, 0., 0.0156001, 0., 0.0156001, 0., 0.0156001, 0., 0.0156001, 0.0156001, 0., 0.0156001}

Timing demonstrates the same results as you mentioned.

Table[test[n, AbsoluteTiming], {n, 10, 150, 10}]

{0.000288766, 0.000618198, 0.00138239, 0.000902868, 0.00133938, 0.00194295, 0.00266179, 0.00352633, 0.00452429, 0.00542013, 0.00635167, 0.0108645, 0.0140152, 0.0164415, 0.0189912}

AbsoluteTiming seems to give the precision we want, but repeated testing of it will likely show a fair bit of variation.

Table[test[n, RepeatedTiming[#, 5] &], {n, 10, 150, 10}]

{5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9, 5.4*10^-9}

And RepeatedTiming shows us something that at first glance might seem odd. It seems that the result is being cached, so repeatedly timing the same LinearSolve expression gives, on average, very little time.

Since you're probably primarily concerned with how long it takes to initially generate the solution, it seems like AbsoluteTiming is the way to go for this problem.

Note however that Timing and AbsoluteTiming are fundamentally limited by the timing mechanisms available to your CPU. In most cases, that's going to be millisecond scales (you can check with $TimeUnit), so as Henrik Schumacher commented, timings smaller than 1ms are probably not super trustworthy.

| improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ You may want to look also at the $TimeUnit documentation. $\endgroup$ – FJRA May 6 '18 at 16:10
  • $\begingroup$ @FJRA Thanks, added it to the answer. $\endgroup$ – eyorble May 6 '18 at 16:17

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.