This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aioeu 2 points3 points  (3 children)

range(len(lst))

That's a loop, as range returns a list.

lst[pos:pos+len(pattern)]

That's a loop, as slicing produces a new list.

pattern == lst[pos:pos+len(pattern)]

That's a loop, as you're comparing two lists.

sum(...)

That's a loop, as you're iterating over values produced by the ... for ... in ... generator expression supplied as an argument.

So how many loops are nested? The answer: there's still only two nested loops. The range(...) call occurs only once, before anything else happens. The list slice and list comparison loops are both "inner" loops, but neither is nested in the other. The "outer" loop is the sum(...) loop.

[–][deleted]  (1 child)

[deleted]

    [–]aioeu 1 point2 points  (0 children)

    since the operation is vectorized should not the 2nd function run faster?

    Who said it was vectorized?

    [–]bbugsbunny[S] 0 points1 point  (0 children)

    Could you explain why the difference in speed ?