Performance-tuning a simple 2D Table construct

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I've recently been playing with some performance tuning over a seemingly simple construction where I build a 2D array of Sin[x*y] for x, yâÂÂInterval[0, 2ÃÂ, 0, 2ÃÂ].
I think it's also a good example of how vastly different results one can get depending on how efficiently the operation is performed.
Here's the operation in the simplest/least efficient way I could think up:
Table[N@Sin[x*y],
x, 0, 2ÃÂ, ÃÂ/250,
y, 0, 2ÃÂ, ÃÂ/250
]; // RepeatedTiming // First
2.01
Now, how can we speed this up? (or slow it down in non-intuitive ways)
For reference, the best I managed to get was 0.0011 but all answers are good answers, especially if they provide analysis of how they speed up/slow down the problem.
Also feel free to cut down on the number of points 250 was simply most evocative.
performance-tuning
add a comment |Â
up vote
1
down vote
favorite
I've recently been playing with some performance tuning over a seemingly simple construction where I build a 2D array of Sin[x*y] for x, yâÂÂInterval[0, 2ÃÂ, 0, 2ÃÂ].
I think it's also a good example of how vastly different results one can get depending on how efficiently the operation is performed.
Here's the operation in the simplest/least efficient way I could think up:
Table[N@Sin[x*y],
x, 0, 2ÃÂ, ÃÂ/250,
y, 0, 2ÃÂ, ÃÂ/250
]; // RepeatedTiming // First
2.01
Now, how can we speed this up? (or slow it down in non-intuitive ways)
For reference, the best I managed to get was 0.0011 but all answers are good answers, especially if they provide analysis of how they speed up/slow down the problem.
Also feel free to cut down on the number of points 250 was simply most evocative.
performance-tuning
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I've recently been playing with some performance tuning over a seemingly simple construction where I build a 2D array of Sin[x*y] for x, yâÂÂInterval[0, 2ÃÂ, 0, 2ÃÂ].
I think it's also a good example of how vastly different results one can get depending on how efficiently the operation is performed.
Here's the operation in the simplest/least efficient way I could think up:
Table[N@Sin[x*y],
x, 0, 2ÃÂ, ÃÂ/250,
y, 0, 2ÃÂ, ÃÂ/250
]; // RepeatedTiming // First
2.01
Now, how can we speed this up? (or slow it down in non-intuitive ways)
For reference, the best I managed to get was 0.0011 but all answers are good answers, especially if they provide analysis of how they speed up/slow down the problem.
Also feel free to cut down on the number of points 250 was simply most evocative.
performance-tuning
I've recently been playing with some performance tuning over a seemingly simple construction where I build a 2D array of Sin[x*y] for x, yâÂÂInterval[0, 2ÃÂ, 0, 2ÃÂ].
I think it's also a good example of how vastly different results one can get depending on how efficiently the operation is performed.
Here's the operation in the simplest/least efficient way I could think up:
Table[N@Sin[x*y],
x, 0, 2ÃÂ, ÃÂ/250,
y, 0, 2ÃÂ, ÃÂ/250
]; // RepeatedTiming // First
2.01
Now, how can we speed this up? (or slow it down in non-intuitive ways)
For reference, the best I managed to get was 0.0011 but all answers are good answers, especially if they provide analysis of how they speed up/slow down the problem.
Also feel free to cut down on the number of points 250 was simply most evocative.
performance-tuning
performance-tuning
edited 39 mins ago
asked 45 mins ago
b3m2a1
25.2k254148
25.2k254148
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
3
down vote
Use packed arrays, and vector operations. Here are 2 possibilities:
Table[N@Sin[x*y],x,0,2ÃÂ,ÃÂ/12,y,0,2ÃÂ,ÃÂ/12]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ Outer[Times, pa, pa]
]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming//First
0.0025
0.0000322
0.0000314
Update
It seems that converting to a packed array takes the most time, so here is a version that avoids packing:
With[pa = Range[0, 24] N[Pi/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming //First
8.3*10^-6
I like the use ofKroneckerProduct. I hadn't thought to use that.
â b3m2a1
37 mins ago
1
Range[0., 2 [Pi], 2 [Pi]/ 500]orSubdivide[0., 2 Pi, 500]are even faster because they generate packed arrays right from the start.
â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking withSubdivide. My brain just immediately jumps toRangebut there's really no reason for that. It also looks likeSubdivideblowsRangeout of the water here...
â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...Range[0., 2. [Pi], [Pi]/250]is much slower thanRange[0., 2. [Pi], [Pi]/250.]
â b3m2a1
16 mins ago
add a comment |Â
up vote
2
down vote
With[r = Range[0., 2Pi, Pi/12.] , Sin @ Outer[Times, r, r]];// RepeatedTiming// First
0.000014
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Use packed arrays, and vector operations. Here are 2 possibilities:
Table[N@Sin[x*y],x,0,2ÃÂ,ÃÂ/12,y,0,2ÃÂ,ÃÂ/12]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ Outer[Times, pa, pa]
]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming//First
0.0025
0.0000322
0.0000314
Update
It seems that converting to a packed array takes the most time, so here is a version that avoids packing:
With[pa = Range[0, 24] N[Pi/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming //First
8.3*10^-6
I like the use ofKroneckerProduct. I hadn't thought to use that.
â b3m2a1
37 mins ago
1
Range[0., 2 [Pi], 2 [Pi]/ 500]orSubdivide[0., 2 Pi, 500]are even faster because they generate packed arrays right from the start.
â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking withSubdivide. My brain just immediately jumps toRangebut there's really no reason for that. It also looks likeSubdivideblowsRangeout of the water here...
â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...Range[0., 2. [Pi], [Pi]/250]is much slower thanRange[0., 2. [Pi], [Pi]/250.]
â b3m2a1
16 mins ago
add a comment |Â
up vote
3
down vote
Use packed arrays, and vector operations. Here are 2 possibilities:
Table[N@Sin[x*y],x,0,2ÃÂ,ÃÂ/12,y,0,2ÃÂ,ÃÂ/12]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ Outer[Times, pa, pa]
]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming//First
0.0025
0.0000322
0.0000314
Update
It seems that converting to a packed array takes the most time, so here is a version that avoids packing:
With[pa = Range[0, 24] N[Pi/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming //First
8.3*10^-6
I like the use ofKroneckerProduct. I hadn't thought to use that.
â b3m2a1
37 mins ago
1
Range[0., 2 [Pi], 2 [Pi]/ 500]orSubdivide[0., 2 Pi, 500]are even faster because they generate packed arrays right from the start.
â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking withSubdivide. My brain just immediately jumps toRangebut there's really no reason for that. It also looks likeSubdivideblowsRangeout of the water here...
â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...Range[0., 2. [Pi], [Pi]/250]is much slower thanRange[0., 2. [Pi], [Pi]/250.]
â b3m2a1
16 mins ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Use packed arrays, and vector operations. Here are 2 possibilities:
Table[N@Sin[x*y],x,0,2ÃÂ,ÃÂ/12,y,0,2ÃÂ,ÃÂ/12]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ Outer[Times, pa, pa]
]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming//First
0.0025
0.0000322
0.0000314
Update
It seems that converting to a packed array takes the most time, so here is a version that avoids packing:
With[pa = Range[0, 24] N[Pi/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming //First
8.3*10^-6
Use packed arrays, and vector operations. Here are 2 possibilities:
Table[N@Sin[x*y],x,0,2ÃÂ,ÃÂ/12,y,0,2ÃÂ,ÃÂ/12]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ Outer[Times, pa, pa]
]; //RepeatedTiming//First
With[pa=Developer`ToPackedArray @ N @ Range[0,2ÃÂ,ÃÂ/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming//First
0.0025
0.0000322
0.0000314
Update
It seems that converting to a packed array takes the most time, so here is a version that avoids packing:
With[pa = Range[0, 24] N[Pi/12],
Sin @ KroneckerProduct[pa, pa]
]; //RepeatedTiming //First
8.3*10^-6
edited 25 mins ago
answered 38 mins ago
Carl Woll
63.4k282163
63.4k282163
I like the use ofKroneckerProduct. I hadn't thought to use that.
â b3m2a1
37 mins ago
1
Range[0., 2 [Pi], 2 [Pi]/ 500]orSubdivide[0., 2 Pi, 500]are even faster because they generate packed arrays right from the start.
â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking withSubdivide. My brain just immediately jumps toRangebut there's really no reason for that. It also looks likeSubdivideblowsRangeout of the water here...
â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...Range[0., 2. [Pi], [Pi]/250]is much slower thanRange[0., 2. [Pi], [Pi]/250.]
â b3m2a1
16 mins ago
add a comment |Â
I like the use ofKroneckerProduct. I hadn't thought to use that.
â b3m2a1
37 mins ago
1
Range[0., 2 [Pi], 2 [Pi]/ 500]orSubdivide[0., 2 Pi, 500]are even faster because they generate packed arrays right from the start.
â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking withSubdivide. My brain just immediately jumps toRangebut there's really no reason for that. It also looks likeSubdivideblowsRangeout of the water here...
â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...Range[0., 2. [Pi], [Pi]/250]is much slower thanRange[0., 2. [Pi], [Pi]/250.]
â b3m2a1
16 mins ago
I like the use of
KroneckerProduct. I hadn't thought to use that.â b3m2a1
37 mins ago
I like the use of
KroneckerProduct. I hadn't thought to use that.â b3m2a1
37 mins ago
1
1
Range[0., 2 [Pi], 2 [Pi]/ 500] or Subdivide[0., 2 Pi, 500] are even faster because they generate packed arrays right from the start.â Henrik Schumacher
22 mins ago
Range[0., 2 [Pi], 2 [Pi]/ 500] or Subdivide[0., 2 Pi, 500] are even faster because they generate packed arrays right from the start.â Henrik Schumacher
22 mins ago
@HenrikSchumacher oh good thinking with
Subdivide. My brain just immediately jumps to Range but there's really no reason for that. It also looks like Subdivide blows Range out of the water here...â b3m2a1
20 mins ago
@HenrikSchumacher oh good thinking with
Subdivide. My brain just immediately jumps to Range but there's really no reason for that. It also looks like Subdivide blows Range out of the water here...â b3m2a1
20 mins ago
@HenrikSchumacher another odd subtle point...
Range[0., 2. [Pi], [Pi]/250] is much slower than Range[0., 2. [Pi], [Pi]/250.]â b3m2a1
16 mins ago
@HenrikSchumacher another odd subtle point...
Range[0., 2. [Pi], [Pi]/250] is much slower than Range[0., 2. [Pi], [Pi]/250.]â b3m2a1
16 mins ago
add a comment |Â
up vote
2
down vote
With[r = Range[0., 2Pi, Pi/12.] , Sin @ Outer[Times, r, r]];// RepeatedTiming// First
0.000014
add a comment |Â
up vote
2
down vote
With[r = Range[0., 2Pi, Pi/12.] , Sin @ Outer[Times, r, r]];// RepeatedTiming// First
0.000014
add a comment |Â
up vote
2
down vote
up vote
2
down vote
With[r = Range[0., 2Pi, Pi/12.] , Sin @ Outer[Times, r, r]];// RepeatedTiming// First
0.000014
With[r = Range[0., 2Pi, Pi/12.] , Sin @ Outer[Times, r, r]];// RepeatedTiming// First
0.000014
answered 32 mins ago
kglr
169k8192395
169k8192395
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f185048%2fperformance-tuning-a-simple-2d-table-construct%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password