Why is time difference on calculating this so big?
python
Solution
>>> import dis
>>> method1 = lambda: (127 / 60) * 60
>>> method2 = lambda: 127 - 127 % 60
>>> dis.dis(method1)
1 0 LOAD_CONST 1 (127)
3 LOAD_CONST 2 (60)
6 BINARY_DIVIDE
7 LOAD_CONST 2 (60)
10 BINARY_MULTIPLY
11 RETURN_VALUE
>>> dis.dis(method2)
1 0 LOAD_CONST 1 (127)
3 LOAD_CONST 3 (7)
6 BINARY_SUBTRACT
7 RETURN_VALUE
In the second case, the modulo operation is simply optimized away.
Problem
I need to round lots of UNIX timestamps down to their respective minutes (expressed as timestamp again). Out of pure curiosity I timed two methods: ``` %timeit (127/60)*60 10000000 loops, best of 3: 76.2 ns per loop %timeit 127 - 127%60 10000000 loops, best of 3: 34.1 ns per loop ``` I ran this several times and second method is consistently around twice as fast as first one. Why is the difference so big?