For a class I'm in we are asked to write out a brute force method for finding 2 elements in lists S1, S2, that add to a specified value x. So far I have it written out as such:
@timing
def brute_force_nested(x, s1 : list, s2 : list) -> bool:
for a in s2:
for b in s1:
if a + b == x:
return True
return False
@timing
def brute_force_inline(x, s1 : list, s2 : list) -> bool:
return bool([ (a,b) for a in s2 for b in s1 if a + b == x ])
But when I run them in the terminal, I get a very large difference in times:
>>> brute_force_nested(5123, S1, S2):
func:brute_force_nested took: 0.007085800170898438 sec and gave: True
>>>func:brute_force_inline(5123, S1, S2)
func:brute_force took: 3.0208868980407715 sec and gave: True
Why is this happening? I was under the impression that the inline syntax was essentialy just syntactical sugar for written out nested loops, but something is obviously different, and I don't know what.