Skip to main content

strange Strange behavior of mathematicaMathematica regarding calculation time

todayToday I witnessed the following strange behavior of mathematicaMathematica, when it comes to calculation time involving larger nested lists. The following is the short example that I setup, I am sure one can reproduce that behavior with other functions:

div[x_] := (RotateLeft[#, {1, 0}] - #) & @ x[[;; , ;; , 1]] + (RotateLeft[#, {0, 1}] - #) & @ x[[;; , ;; , 2]]

Which is some kind of discrete divergence operator, but this does not really matter here. And then I defined the corresponding lists to be evaluated by div:

testArray = Table[RandomReal[], {i, 1, 256}, {j, 1, 256}];
testArray2D = Table[{testArray[[i, j]], testArray[[j, i]]}, {i, Dimensions[testArray][[1]]}, {j, Dimensions[testArray][[2]]}];
testArray2Dnew = Table[{RandomReal[], RandomReal[]}, {i, 1, 256}, {j, 1, 256}];

It is obvious, that testArray2D and testArray2Dnew should be pretty much identical from a numerical standpoint (i.e. same dimensions, same kind of numbers and same precision). But when I apply div on both of them I get completely different calculation times:

Timing[div[testArray2D];]

delivers 0.035085 seconds and

Timing[div[testArray2Dnew];]

delivers 0.006100 seconds

Executing the above Timing commands over and over again does not change the results, so there is about a factor of 6 regarding calculation time.

Can somebody explain that behavior to me, because right now I am just confused.

strange behavior of mathematica regarding calculation time

today I witnessed the following strange behavior of mathematica, when it comes to calculation time involving larger nested lists. The following is the short example that I setup, I am sure one can reproduce that behavior with other functions:

div[x_] := (RotateLeft[#, {1, 0}] - #) & @ x[[;; , ;; , 1]] + (RotateLeft[#, {0, 1}] - #) & @ x[[;; , ;; , 2]]

Which is some kind of discrete divergence operator, but this does not really matter here. And then I defined the corresponding lists to be evaluated by div:

testArray = Table[RandomReal[], {i, 1, 256}, {j, 1, 256}];
testArray2D = Table[{testArray[[i, j]], testArray[[j, i]]}, {i, Dimensions[testArray][[1]]}, {j, Dimensions[testArray][[2]]}];
testArray2Dnew = Table[{RandomReal[], RandomReal[]}, {i, 1, 256}, {j, 1, 256}];

It is obvious, that testArray2D and testArray2Dnew should be pretty much identical from a numerical standpoint (i.e. same dimensions, same kind of numbers and same precision). But when I apply div on both of them I get completely different calculation times:

Timing[div[testArray2D];]

delivers 0.035085 seconds and

Timing[div[testArray2Dnew];]

delivers 0.006100 seconds

Executing the above Timing commands over and over again does not change the results, so there is about a factor of 6 regarding calculation time.

Can somebody explain that behavior to me, because right now I am just confused.

Strange behavior of Mathematica regarding calculation time

Today I witnessed the following strange behavior of Mathematica, when it comes to calculation time involving larger nested lists. The following is the short example that I setup, I am sure one can reproduce that behavior with other functions:

div[x_] := (RotateLeft[#, {1, 0}] - #) & @ x[[;; , ;; , 1]] + (RotateLeft[#, {0, 1}] - #) & @ x[[;; , ;; , 2]]

Which is some kind of discrete divergence operator, but this does not really matter here. And then I defined the corresponding lists to be evaluated by div:

testArray = Table[RandomReal[], {i, 1, 256}, {j, 1, 256}];
testArray2D = Table[{testArray[[i, j]], testArray[[j, i]]}, {i, Dimensions[testArray][[1]]}, {j, Dimensions[testArray][[2]]}];
testArray2Dnew = Table[{RandomReal[], RandomReal[]}, {i, 1, 256}, {j, 1, 256}];

It is obvious, that testArray2D and testArray2Dnew should be pretty much identical from a numerical standpoint (i.e. same dimensions, same kind of numbers and same precision). But when I apply div on both of them I get completely different calculation times:

Timing[div[testArray2D];]

delivers 0.035085 seconds and

Timing[div[testArray2Dnew];]

delivers 0.006100 seconds

Executing the above Timing commands over and over again does not change the results, so there is about a factor of 6 regarding calculation time.

Can somebody explain that behavior to me, because right now I am just confused.

Tweeted twitter.com/#!/StackMma/status/364761161423536128
Source Link
Wizard
  • 2.7k
  • 18
  • 28

strange behavior of mathematica regarding calculation time

today I witnessed the following strange behavior of mathematica, when it comes to calculation time involving larger nested lists. The following is the short example that I setup, I am sure one can reproduce that behavior with other functions:

div[x_] := (RotateLeft[#, {1, 0}] - #) & @ x[[;; , ;; , 1]] + (RotateLeft[#, {0, 1}] - #) & @ x[[;; , ;; , 2]]

Which is some kind of discrete divergence operator, but this does not really matter here. And then I defined the corresponding lists to be evaluated by div:

testArray = Table[RandomReal[], {i, 1, 256}, {j, 1, 256}];
testArray2D = Table[{testArray[[i, j]], testArray[[j, i]]}, {i, Dimensions[testArray][[1]]}, {j, Dimensions[testArray][[2]]}];
testArray2Dnew = Table[{RandomReal[], RandomReal[]}, {i, 1, 256}, {j, 1, 256}];

It is obvious, that testArray2D and testArray2Dnew should be pretty much identical from a numerical standpoint (i.e. same dimensions, same kind of numbers and same precision). But when I apply div on both of them I get completely different calculation times:

Timing[div[testArray2D];]

delivers 0.035085 seconds and

Timing[div[testArray2Dnew];]

delivers 0.006100 seconds

Executing the above Timing commands over and over again does not change the results, so there is about a factor of 6 regarding calculation time.

Can somebody explain that behavior to me, because right now I am just confused.