|
View:
New views
4 Messages
—
Rating Filter:
Alert me
|
|
|
Newbiew need help optimizing code and understanding OctaveHi All,
Below is the code I am writing as a part of a bigger project. My Issues are I am an octave newbie and I used to do programming in C/C++ so I write octave code pretty similar to C/C++ code. The code below runs really slow (it's not that complex though), so I would like tips from all on how to make it fast. I know I need to vectorize this but I don't really understand the concept of vectorization and so need expert advice. Any help would be really appreciated.. Thanks h = 80; r = 3.5; s = 12; xmin = 0; xmax = 120; widthstrip = 36; for widthstrip = 82:2:82 ymin = (h/2) - (widthstrip/2); ymax = (h/2) + (widthstrip/2); Fd12 = 0; sum = 0; count = 0; for i = xmin:xmax for j = ymin:ymax x = i; y = j; S = s / r; X = x / r; Y = y / r; H = h / r; A = X^2 + Y^2 + S^2; B = S^2 + X^2; C = (H - Y)^2; part1 = S / B; part2 = S / (2 * B * pi); part3 = acos( (Y^2 - B + 1) / (A - 1) ); part4 = acos( (C - B + 1) / (C + B - 1) ); part5 = (A + 1) / (sqrt( (A - 1)^2 + 4 * Y^2 )); part6 = acos( (Y^2 - B + 1) / (sqrt(B) * (A - 1)) ); part7 = (C + B + 1) / (sqrt( (C + B - 1)^2 + 4 * C )); part8 = acos( (C - B + 1) / (sqrt(B) * (C + B - 1)) ); part9 = H * acos( (1/sqrt(B)) ); sum = sum + (part1 - (part2 * (part3 + part4 - (Y * part5 * part6) - (sqrt(C) * part7 * part8) + part9))); ++count; endfor endfor Fd12 = sum; printf("\nWidth of %d inch Fd1-2 = %f, Count is %d and Fd1-2 final = %f ", widthstrip, Fd12,count,Fd12/count); endfor |
|
|
Re: Newbiew need help optimizing code and understanding OctaveJust a correction the loop should be
for widthstrip = 36:2:84
|
|
|
Re: Newbiew need help optimizing code and understanding OctaveAm Montag, den 31.03.2008, 09:59 -0700 schrieb malhotrag: > > The code below runs really slow (it's not that complex though), so I > would > like tips from all on how to make it fast. I know I need to vectorize > this > but I don't really understand the concept of vectorization and so need > expert advice. I'm not an expert myself, but here is a short explanation of vectorization: you got an inner loop: for j = ymin:ymax y = j; Y = y / r; ..... end for; You got a loop variable j, which will be ymin, then ymin+1, then ... then ymax. for every iteration of the code you compute a new value Y = j / r; to vectorize that, one would use an vector that contains all the values over which j would loop: y=ymin:ymax; # in C this would be an array. then you divide the vector: Y=y/r; # this operation is carried out for every element in the y vector. when you perform stuff like Y^2, and Y is now an Vector, you have to write Y.^2 (Y^2 would be the result of a vector multiplication). The point say ovtave that you don't intend a vector multiplication, but want to square the vector (array) element by element. same goes for *, and /. (a vector addition is what you want, so + is ok) there are a few stupid things in your code: x = i; y = j; this stuff slows down the whole thing, as octave is interpreting the code. use x and y as loop variables. eliminate those lines. H = h / r; S = s / r; h, s, and r are initialized at the beginning of your code, but you calculate H and S every iteration of the loop. move these calculations out of the loop(s). -- Thomas Ilnseher <ilnseher@...> _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
|
|
Re: Newbiew need help optimizing code and understanding OctaveOn Apr 1, 2008, at 7:13 AM, malhotrag wrote: > > Just a correction the loop should be > > for widthstrip = 36:2:84 > > malhotrag wrote: >> >> Hi All, >> >> Below is the code I am writing as a part of a bigger project. My >> Issues >> are I am an octave newbie and I used to do programming in C/C++ so >> I write >> octave code pretty similar to C/C++ code. >> >> The code below runs really slow (it's not that complex though), so >> I would >> like tips from all on how to make it fast. I know I need to >> vectorize this >> but I don't really understand the concept of vectorization and so >> need >> expert advice. >> >> Any help would be really appreciated.. >> >> Thanks >> >> h = 80; >> r = 3.5; >> s = 12; >> xmin = 0; >> xmax = 120; >> widthstrip = 36; >> for widthstrip = 82:2:82 >> ymin = (h/2) - (widthstrip/2); >> ymax = (h/2) + (widthstrip/2); >> Fd12 = 0; >> sum = 0; >> count = 0; >> >> for i = xmin:xmax >> for j = ymin:ymax >> x = i; >> y = j; >> S = s / r; >> X = x / r; >> Y = y / r; >> H = h / r; >> >> A = X^2 + Y^2 + S^2; >> B = S^2 + X^2; >> C = (H - Y)^2; >> >> part1 = S / B; >> part2 = S / (2 * B * pi); >> part3 = acos( (Y^2 - B + 1) / (A - 1) ); >> part4 = acos( (C - B + 1) / (C + B - 1) ); >> part5 = (A + 1) / (sqrt( (A - 1)^2 + 4 * Y^2 )); >> part6 = acos( (Y^2 - B + 1) / (sqrt(B) * (A - 1)) ); >> part7 = (C + B + 1) / (sqrt( (C + B - 1)^2 + 4 * C )); >> part8 = acos( (C - B + 1) / (sqrt(B) * (C + B - 1)) ); >> part9 = H * acos( (1/sqrt(B)) ); >> >> sum = sum + (part1 - (part2 * (part3 + part4 - (Y * part5 * >> part6) - >> (sqrt(C) * part7 * part8) + part9))); >> ++count; >> endfor >> endfor >> >> Fd12 = sum; >> >> printf("\nWidth of %d inch Fd1-2 = %f, Count is %d and Fd1-2 final >> = %f >> ", widthstrip, Fd12,count,Fd12/count); >> >> endfor >> > The most effective speed improvement strategy would be to vectorize your algorithm. The idea is to replace I'm guessing you'd begin by creating a single matrix for x and y. x = xmin:xmax; y = ymin:ymax; X = (ones (numel(x), 1) * x) / r; Y = (ones (numel(y), 1) * y) / r; S = S / r; A = X.^2 + Y.^2 + S.^2; ... etc ... Ben _______________________________________________ Help-octave mailing list Help-octave@... https://www.cae.wisc.edu/mailman/listinfo/help-octave |
| Free embeddable forum powered by Nabble | Forum Help |