comp.lang.idl-pvwave archive - RDF feed
type: channel
description: Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado
items: for loop is killing me
items: Re: for loop is killing me

for loop is killing me
type: item
description: Hello all, help me! I am running a code 'infested' with for loops.And as u can guess, its painfully slow. The part of my code with loop looks like the following: jump1: ;Setting up the Green functions array which will have the same number of elements as of amp_vec G=fltarr(n_elements(amp_vec),n_elements(amp_vec)) G_phi=fltarr(n_elements(amp_vec)) G_rp=fltarr(n_elements(amp_vec)) G_r=fltarr(n_elements(amp_vec)) G_in=fltarr(n_elements(amp_vec)) G_out=fltarr(n_elements(amp_vec)) G_p=fltarr(n_elements(amp_vec)) G_h=fltarr(n_elements(amp_vec)) gp=fltarr(n_elements(amp_vec)) gh=fltarr(n_elements(amp_vec)) G_in_msum = fltarr(n_elements(amp_vec)) G_out_msum = fltarr(n_elements(amp_vec)) G_in_phisum = fltarr(n_elements(amp_vec)) G_out_phisum = fltarr(n_elements(amp_vec)) if ignore_realdata eq 1. then begin amp_vec= abs(randomn(0.5,n_radial_points,/double)) a=1. ;print,' amp_vec is',amp_vec endif do_u_like_to_start_with_rp_loop = 0 ; Set 1 if YES,0 if NO if do_u_like_to_start_with_rp_loop eq 1. then BEGIN PRINT,' WE ARE USING rp PREFERED LOOP' goto,jump2 ENDIF PRINT,' WE ARE USING r PREFERED LOOP' for i_r= 0,n_radial_points-1 do begin loop_time = systime(1) if ignore_realdata eq 1. then begin r_scld=( i_r*2.)/(n_radial_points -1) ;---------> We took off trap radius from here for scaling purpose,see note ;---------> Also note the use of factor 2. This is because, we want to get values outside ;---------- > the cylinder. But this is just for demonstration purpose!!!! endif else begin r_scld=( i_r)/(n_radial_points -1) ; took off the factor 2 endelse ;r_scld = 2. ;print,'r_scld value is ',r_scld for i_rp= 0,n_radial_points-1 do begin if ignore_realdata eq 1. then begin rp_mat = randomn(1,n_radial_points,/double) ;print,'rp_mat is',rp_mat rp_scld = rp_mat(i_rp) endif else begin ;rp_scld=( i_rp/((n_radial_points-1))) * float(amp_vec(i_rp)) rp_scld= float(amp_vec(i_rp)) ;print,' amp_vec is',amp_vec endelse ; rp_scld=( i_rp/((n_radial_points-1))) * float(amp_vec(i_rp)) ;rp_mat = ran…
creator: tarequeaziz
date: 2007-11-06T06:31:24-00:00

Re: for loop is killing me
type: item
description: Okay, here's one more for you. Your m loop and phi loop can both be made for loop-less. Here's your warning though: I haven't tested this code in the slightest. It's hard to test stuff when it is part of a much larger program, so I'll leave that to you. I'll just explain what this chunk of code is doing. So this replaces your m and your phi loops: i_phi_max = 20. phis = rebin(findgen(i_phi_max)+1,i_phi_max,i_phi_max) i_ms = rebin(findgen(1,i_phi_max)+1,i_phi_max,i_phi_max) gp = 2.0*(1./i_ms)*(r_minus/r_plus)^i_ms * cos(i_ms * phis*2*!PI/ i_phi_max ) gh = 2.0*(1./i_ms)*(r_minus*r_plus/a^2)^i_ms * cos(i_ms * phis*2*!PI/ i_phi_max ) G_in_phisum = total(gp,1) G_out_phisum = total(gh,1) The general idea here is to simply do all the calculations at once, combined into one big array operation. So for instance from looking at the phi loop you know that your phi's are going to go from 1-20 and are going to be used 20 times by the m-loop. Imagine this as a 20x20 array where each row contains the numbers 1-20. The second line (phis = rebin()) creates such a 20x20 array. Rebin is used to repeat the findgen() 20 times vertically. Next consider your i_m's. These will also have the values 1-20 and will be used against the different values in the phis array. So you can consider it to be a 20x20 array where each column has the values 1-20. Once again, this array is created using rebin and findgen in a very similar way to how I created the phi array. Once this is accomplished you have two 20x20 arrays with all the 400 possible combinations of phi and m. Then, you perform your calculations exactly as you did in the for loop and in one go calculate all the possible results. Finally you use the dimension keyword to the total function to sum up along rows. Viola, no more for loops! To make sure things are clear, I want to explain this in a different way: The idea is to do all your calculations at once. To make this happen I created a 20x20 array for the phi loop and a 20x20 array for the…
creator: Conor
date: 2007-11-08T15:26:27-00:00

Re: for loop is killing me
type: item
description: Okay, here's one more for you. Your m loop and phi loop can both be made for loop-less. Here's your warning though: I haven't tested this code in the slightest. It's hard to test stuff when it is part of a much larger program, so I'll leave that to you. I'll just explain what this chunk of code is doing. So this replaces your m and your phi loops: i_phi_max = 20. phis = rebin(findgen(i_phi_max)+1,i_phi_max,i_phi_max) i_ms = rebin(findgen(1,i_phi_max)+1,i_phi_max,i_phi_max) gp = 2.0*(1./i_ms)*(r_minus/r_plus)^i_ms * cos(i_ms * phis*2*!PI/ i_phi_max ) gh = 2.0*(1./i_ms)*(r_minus*r_plus/a^2)^i_ms * cos(i_ms * phis*2*!PI/ i_phi_max ) G_in_phisum = total(gp,1) G_out_phisum = total(gh,1) The general idea here is to simply do all the calculations at once, combined into one big array operation. So for instance from looking at the phi loop you know that your phi's are going to go from 1-20 and are going to be used 20 times by the m-loop. Imagine this as a 20x20 array where each row contains the numbers 1-20. The second line (phis = rebin()) creates such a 20x20 array. Rebin is used to repeat the findgen() 20 times vertically. Next consider your i_m's. These will also have the values 1-20 and will be used against the different values in the phis array. So you can consider it to be a 20x20 array where each column has the values 1-20. Once again, this array is created using rebin and findgen in a very similar way to how I created the phi array. Once this is accomplished you have two 20x20 arrays with all the 400 possible combinations of phi and m. Then, you perform your calculations exactly as you did in the for loop and in one go calculate all the possible results. Finally you use the dimension keyword to the total function to sum up along rows. Viola, no more for loops! To make sure things are clear, I want to explain this in a different way: The idea is to do all your calculations at once. To make this happen I created a 20x20 array for the phi loop and a 20x20 array for the…
creator: Conor
date: 2007-11-08T19:26:37-00:00