Prev: shadow in images
Next: HSV Quantization
From: Andreas Lobinger on 14 Jun 2010 05:36 Aloha, i'm stil wonder, what exactly bsxfun does and more and more i have some doubts, that the profile reports correctly... The following example shows some angle calculation, for random points the angle towards a reference orientation is calculated. For further processing the result angle is reduced to a -pi;pi range. n_points = 1000*100; n_tests = 100; net = struct(); net.orientation = [exp(-i*pi*(0:2/3:4/3))]; neto = conj(net.orientation); tic for k=1:n_tests e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; % regular way -> angle, subtract orientation, reduce to [-pi;pi]; w1 = angle(e1); a1 = bsxfun(@minus,w1,angle(net.orientation)); a1m = (mod(a1+3*pi,2*pi)-pi); end toc tic for k=1:n_tests e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; % new -> complex mult with conj(orientation), angle a2 = bsxfun(@times,e1, neto); a2m = angle(a2); end toc In general this works and the a1m and a2m differ only in the 10e-15 range (if random seed is set correctly). The runtime reported by tic/toc is (here: R2009a) ~6.5s for the first, 7.0s for the second part. The interesting thing is the profile output. time calls line 1 1 n_points = 1000*100; 1 2 n_tests = 100; 3 1 4 net = struct(); 1 5 net.orientation = [exp(-i*pi*(0:2/3:4/3))]; 1 6 neto = conj(net.orientation); 7 1 8 tic 1 9 for k=1:n_tests 0.89 100 10 e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; 11 12 % regular way -> angle, subtract orientation, reduce to [-pi;pi]; 1.95 100 13 w1 = angle(e1); 0.33 100 14 a1 = bsxfun(@minus,w1,angle(net.orientation)); 15 3.48 100 16 a1m = (mod(a1+3*pi,2*pi)-pi); 100 17 end 1 18 toc 19 1 20 tic 1 21 for k=1:n_tests 0.95 100 22 e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; 23 24 % new -> complex mult with conj(orientation), angle 0.80 100 25 a2 = bsxfun(@times,e1, neto); 5.31 100 26 a2m = angle(a2); 27 0.02 100 28 end 1 29 toc Apart from small variations, the call to angle in the second part costs 5.3s, while in the first part only 1.9s, with both the same size matrices. My example in the thread 'bsxfun slow' was similar. I have no doubt, that bsxfun can be the faster solution, but from the profiler output it seems that the bsxfun output introduces impact following functions. Any ideas? Wishing a happy day, LOBI
From: Steven Lord on 14 Jun 2010 09:43 "Andreas Lobinger" <tvask(a)biszumknie.de> wrote in message news:hv4t63$m5m$1(a)fred.mathworks.com... > Aloha, > > i'm stil wonder, what exactly bsxfun does and more and more i have some > doubts, that the profile reports correctly... > > The following example shows some angle calculation, for random points the > angle towards a reference orientation is calculated. For further > processing the result angle is reduced to a -pi;pi range. > > n_points = 1000*100; > n_tests = 100; > > net = struct(); > net.orientation = [exp(-i*pi*(0:2/3:4/3))]; > neto = conj(net.orientation); So neto is 1-by-3. > tic > for k=1:n_tests > e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; e1 is 100000-by-1. > % regular way -> angle, subtract orientation, reduce to [-pi;pi]; > w1 = angle(e1); This computes the angle of a 100000-by-1 vector. > a1 = bsxfun(@minus,w1,angle(net.orientation)); The result of this is 100000-by-3. > a1m = (mod(a1+3*pi,2*pi)-pi); > end > toc > > tic > for k=1:n_tests > e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; This is 100000-by-1. > % new -> complex mult with conj(orientation), angle > a2 = bsxfun(@times,e1, neto); The result of this is 100000-by-3. > a2m = angle(a2); This computes the ANGLE for each element of a 100000-by-3 matrix. > end > toc > > In general this works and the a1m and a2m differ only in the 10e-15 range > (if random seed is set correctly). > The runtime reported by tic/toc is (here: R2009a) ~6.5s for the first, > 7.0s for > the second part. > > The interesting thing is the profile output. > time calls line > 1 1 n_points = 1000*100; 1 2 n_tests = 100; 3 1 4 net > = struct(); 1 5 net.orientation = [exp(-i*pi*(0:2/3:4/3))]; 1 6 neto > = conj(net.orientation); 7 1 8 tic 1 9 for k=1:n_tests 0.89 100 > 10 e1 = -10-10i + rand(n_points,1) * 20 + rand(n_points,1)*20i; 11 > 12 % regular way -> angle, subtract orientation, reduce to [-pi;pi]; > 1.95 100 13 w1 = angle(e1); 0.33 100 14 a1 = > bsxfun(@minus,w1,angle(net.orientation)); 15 3.48 100 16 a1m > = (mod(a1+3*pi,2*pi)-pi); 100 17 end 1 18 toc 19 1 20 tic 1 21 for > k=1:n_tests 0.95 100 22 e1 = -10-10i + rand(n_points,1) * 20 + > rand(n_points,1)*20i; 23 24 % new -> complex mult with > conj(orientation), angle > 0.80 100 25 a2 = bsxfun(@times,e1, neto); 5.31 100 26 > a2m = angle(a2); 27 0.02 100 28 end 1 29 toc > > Apart from small variations, the call to angle in the second part costs > 5.3s, while in the first part only 1.9s, with both the same size matrices. Line 26 is operating on 3 times as much data as line 13, so you might expect it to take about 3 times as long. [It's not exactly 3 times as long for reasons I'm not going to get into.] > My example in the thread 'bsxfun slow' was similar. I have no doubt, that > bsxfun can be the faster solution, but from the profiler output it seems > that the bsxfun output introduces impact following functions. Certainly, for the simple reason that calling BSXFUN first gives ANGLE more data on which it needs to operate than calling ANGLE then BSXFUN. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: Andreas Lobinger on 14 Jun 2010 09:57 Aloha, On Mon, 14 Jun 2010 09:43:21 -0400, Steven Lord wrote: > "Andreas Lobinger" <tvask(a)biszumknie.de> wrote in message > news:hv4t63$m5m$1(a)fred.mathworks.com... >> i'm stil wonder, what exactly bsxfun does and more and more i have some >> doubts, that the profile reports correctly... PEBKAC > Certainly, for the simple reason that calling BSXFUN first gives ANGLE > more data on which it needs to operate than calling ANGLE then BSXFUN. Thanks for looking at it (although the question makes no sense). I'll do more research locally before i post the next time. Wishing a happy day, LOBI
|
Pages: 1 Prev: shadow in images Next: HSV Quantization |