Skip to main content
Post Made Community Wiki by Gareth Rees
Remove incorrect explanation (I misunderstood the problem)
Source Link
Gareth Rees
  • 49.7k
  • 3
  • 129
  • 210

Before starting to make timing comparisons, it's important to check that the optimized code is correct. It's no good being a hundred times faster if you get the wrong answer! So let's look at the results of the two functions on a simple test case:

>>> instructions = 'L1, L1, L1, L1, R1, R1, R1, R1'
>>> solve_set(instructions)
0.0
>>> solve_bisect(instructions)
1.0

Oops. It should have been clear that something was up, because any solution to this problem has to decode the instructions at least as far as the first intersection, and the only extra work that solve_set does is to see if the point visited is a member of a set, and to add it if not, and these operations don't take much longer than decoding the instructions, so the most you could possibly save would be around 50%. If you saved 99% of the time, it can only be because you made a mistake and stopped too soon.

Before starting to make timing comparisons, it's important to check that the optimized code is correct. It's no good being a hundred times faster if you get the wrong answer! So let's look at the results of the two functions on a simple test case:

>>> instructions = 'L1, L1, L1, L1, R1, R1, R1, R1'
>>> solve_set(instructions)
0.0
>>> solve_bisect(instructions)
1.0

Oops. It should have been clear that something was up, because any solution to this problem has to decode the instructions at least as far as the first intersection, and the only extra work that solve_set does is to see if the point visited is a member of a set, and to add it if not, and these operations don't take much longer than decoding the instructions, so the most you could possibly save would be around 50%. If you saved 99% of the time, it can only be because you made a mistake and stopped too soon.

Before starting to make timing comparisons, it's important to check that the optimized code is correct. It's no good being a hundred times faster if you get the wrong answer! So let's look at the results of the two functions on a simple test case:

>>> instructions = 'L1, L1, L1, L1, R1, R1, R1, R1'
>>> solve_set(instructions)
0.0
>>> solve_bisect(instructions)
1.0

Oops.

Source Link
Gareth Rees
  • 49.7k
  • 3
  • 129
  • 210

Before starting to make timing comparisons, it's important to check that the optimized code is correct. It's no good being a hundred times faster if you get the wrong answer! So let's look at the results of the two functions on a simple test case:

>>> instructions = 'L1, L1, L1, L1, R1, R1, R1, R1'
>>> solve_set(instructions)
0.0
>>> solve_bisect(instructions)
1.0

Oops. It should have been clear that something was up, because any solution to this problem has to decode the instructions at least as far as the first intersection, and the only extra work that solve_set does is to see if the point visited is a member of a set, and to add it if not, and these operations don't take much longer than decoding the instructions, so the most you could possibly save would be around 50%. If you saved 99% of the time, it can only be because you made a mistake and stopped too soon.