0

I tried to construct in python a functon which includes a for loop to iterate a function that computes a double integral. This is the code:

def bestset():

    sumsqu = [sqres(grid[i]) for i in range(len(grid))]

    index_min = min(xrange(len(sumsqu)), key=sumsqu.__getitem__)

    return index_min

sqres is a function that contains the computation of such double integral while grid is a list of lenght 5^5=3125. The problem of slowness derives from such for loop inside the list. I tried to reduce the lenght of grid to 3^5 elements, but I takes around 20 minutes to perform the loop. Obviously it takes age to perform the for loop with 3125 elements. Is there a way to improve the speed of it in python?

AleB
  • 153
  • 1
  • 3
  • 10
  • 1
    That slowness has nothing to do with this code. – Stefan Pochmann Dec 05 '17 at 19:02
  • Is it related to the power of my machine or rather to the integrals inside? – AleB Dec 05 '17 at 19:04
  • 1
    The more pythonic way to loop over your grid is to "sumsqu = [sqres(element) for element in grid]". Maybe there is some performance boost in this too, but doubtly.. – Markus Dec 05 '17 at 19:06
  • 1
    try memoize output of sqres if grid contain duplicate values – Fabricator Dec 05 '17 at 19:07
  • 1
    @Markus More like `sumsqu = map(sqres, grid)` – Stefan Pochmann Dec 05 '17 at 19:08
  • @StefanPochmann Hmm depends on your preferences, see this [link](https://stackoverflow.com/questions/1247486/python-list-comprehension-vs-map). But of course another and better way, too :) – Markus Dec 05 '17 at 19:17
  • @Markus Well, I think even that style guide that Alex mentions as recommending against `map` doesn't say that. He is and has been a Googler, so I assume he's talking about [this](https://google.github.io/styleguide/pyguide.html?showone=Deprecated_Language_Features#Deprecated_Language_Features) which says don't use `map` with `lambda` ([I totally agree](https://stackoverflow.com/a/30413651/1672429)) and shows `map(math.sqrt, data)` as a "Yes" example. And back when he mentioned it, it [didn't sound much different](https://github.com/google/styleguide/blob/9d234080c5413f3f8c7/pyguide.html). – Stefan Pochmann Dec 05 '17 at 19:45

0 Answers0