The Captain's Room

  • + 1 comment

    Of course your set-based version is slow; you load the entire list into memory and then traverse it again. If you were to use a for loop to iterate over the input, parsing each input and testing set membership - or use a generator comprehension to parse the list and return the values - then you would only be traversing the list once and performing the necessary calculations in constant space.

    Of course, using a generator would speed up either version, but the lack of a generator - the fact that the entire input is loaded, kept in memory and then traversed a second time - is the main reason I'm criticising the solution. (The fact that it's a one-off math trick that is entirely irrelevant to general set problems being the important secondary criticisum).

    How much faster the generator solution will depend a lot on the size of the input and whether that is smaller than the basic filesystem/filecache block. So results will vary.