Ad
  • Custom User Avatar

    I agree with paime. Given that Python uses C under the hood, I assume that the change of the sign is done like in C/C++, that is, the two's compliment. With that in mind, both -n and n*-1 are carried out by the same assembly instruction(optimised by the compiler) and should have on average the same execution time if you perform this an infinite amount of times. The n - n * 2 operation seems unreasonable to be the fastest since it involves two operations. Again, for a couple of executions the difference might not be obvious. But, for a sufficiently large amount of executions, the averages should have a distinct difference with the "n - n * 2" being the slowest. At least, theoretically.