optimization problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jenya56
    New Member
    • Nov 2009
    • 14

    optimization problem

    My program runs but it is soooooooooo slow. I have lots of data files I am reading and this for loop is where the program takes FOREVER.......N ote I declare data_ncdc=[] and ncdc_station=[]. Any suggestions would be appreciated.... . THANKS!!!!!
    Code:
    for il in range(len(data_ncdc)):
       coord_ind = common_index(ave_lon_ncdc,ave_lat_ncdc, data_ncdc[il][0],  data_ncdc[il][1])
         if coord_ind == None:
            ave_lon_ncdc.append(float(data_ncdc[il][0]))
            ave_lat_ncdc.append(float(data_ncdc[il][1]))
            ncdc_station.append(float(data_ncdc[il][3]))
         else:
            ncdc_station[coord_ind[0]].extend(float(data_ncdc[il][3]))
    
    def common_index(list1, list2, val1, val2):
       assert len(list1) == len(list2), "List not of equal length"
       for i in range(len(list1)):
         if list1[i] == val1 and list2[i] == val2:
            return [i]
    Last edited by bvdet; Dec 1 '09, 09:32 PM. Reason: Add code tags
  • Glenton
    Recognized Expert Contributor
    • Nov 2008
    • 391

    #2
    Perhaps if you told us what you were trying to do?!

    A good way to improve speed dramatically is to work with numpy arrays and array operations rather than loops.

    Eg if a is a large list of numbers
    Code:
    a=array(a)
    b=a*2+10
    would be much quicker than
    Code:
    b=[]
    for i in a:
        b.append(2*i+10)
    This is because all the heavy lifting is done in C/C++

    Comment

    Working...