0
$\begingroup$

The following code takes about 3 seconds to evaluate, but it has "only" 1000 values to be computed. I'm solving a problem where I have more than 100,000 values to be evaluated, and it is taking hours to compute.

table = Table[Table[RandomInteger[{0, 10}] (x^i + y ^j) , {i, 1, 2}, {j,i,2}], {k, 1, 1000}];
numerical = Table[ParallelTable[table[[i]], {x, 0, 1, 0.1}, {y, 0, 1, 0.1}], {i,1, Length[table]}] // AbsoluteTiming

How can I speed up the above code?

$\endgroup$
1
  • $\begingroup$ You'll have better luck, I think, by adding a description of what the end goal is here. As it is, it appears to be a complete mess to make a complete mess, where it will almost certainly turn out there's a direct (and efficient) solution to what you're after... $\endgroup$
    – ciao
    Feb 5 '17 at 4:20
5
$\begingroup$

The original code takes ~200 seconds to run on my computer. This one is about 50 times faster.

ParallelTable[Table[Table[RandomInteger[{0, 10}] (x^i + y^j), {i, 1, 2}, {j, i, 2}],{x, 0, 1, 0.1}, {y, 0, 1, 0.1}], {k, 1, 1000}]

(Update) If you want to use the table generated before:

numericalA = Transpose[Transpose /@ ParallelTable[table, {x, 0, 1, 0.1}, {y, 0, 1, 0.1}]]; // AbsoluteTiming
(*1.27956s, 1000 values*)
numericalB = ParallelTable[Table[table[[i]], {x, 0, 1, 0.1}, {y, 0, 1, 0.1}], {i, 1,Length[table]}]; // AbsoluteTiming
(*210.042s, 1000 values*)
numericalA === numericalB
(*True*)

It seems that table[[i]] in the original code is time-consuming. Use lists directly as parameters can usually improve the speed.

$\endgroup$
3
$\begingroup$

[Partial answer]

IF you generate a full tensorial structure, that is, have j go from 1 to 2 rather than from i to 2, and furthermore make {x,y} bounds decimals, then the power sums can be a packed array, and it only need be computed once. The multiplication can be done separately.

AbsoluteTiming[
 xyPairs = 
    Table[x^i + y^j, {x, 0., 1., 0.1}, {y, 0., 1., 0.1}, {i, 1, 
      2}, {j, 1, 2}];]
dims = Dimensions[xyPairs];
AbsoluteTiming[rn = RandomInteger[{0, 10}, Join[{1000}, dims]];]
AbsoluteTiming[res = Map[#*xyPairs &, rn];]

(* Out[198]= {0.0005027, Null}

Out[200]= {0.00776015, Null}

Out[201]= {0.0643741, Null} *)

Now you need to puzzle out how to truncate to get the correct final size.

$\endgroup$

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.