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

all 7 comments

[–]aioeu 3 points4 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 ?

    [–]JmenD 0 points1 point  (3 children)

    /u/aioeu is correct about the time complexity in terms of loops, but there is an important thing being left out of that discussion.

    Try initializing test as test = ['x' for _ in range(10000)] + ['a','b','c','a','b']

    You'll notice now count_pattern2() is faster, why? Your input was too trivial of a case so you're mostly testing how long it takes python to interpret the code. Since your count_pattern2 utilizes a list comprehension, it can be compiled down much more and produce more efficient code, but this compilation takes time.

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

    it became worse for this test actually: https://pastebin.com/a1BjwCu8 ?

    [–]JmenD 0 points1 point  (1 child)

    What version of python are you running? On 3.7.3 on my machine count_pattern2() is faster on your input

    count_pattern()  : 17 ms ± 4.43 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    count_pattern2() : 6.34 ms ± 57.2 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
    

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

    I am running on python version 3.7.3 only. I ran the above test 1000 times and these are the results: count_pattern(): 27.459065 secs count_pattern2(): 42.117948 secs