Ad
  • Custom User Avatar

    @natan, I tested for that. I ran the two functions for thousands of numbers thousands of different times. I tested running them both in the same script. I tested them in separate scripts. I tested them varying the order in which they ran. I am not saying that replacing line breaks with semicolons will make all python code run faster on windows. I am saying that for these two particular functions, there is a consistent 5% difference between them on windows running python 3.11 where that 5% difference does not exist for these particular two functions on a linux system. I have no doubt that this difference might disappear with other code, slightly different code, or even different hardware.

  • Custom User Avatar

    I would not want to describe it as a windows thing. When you measure something you need to make sure it isn't being drowned out by other noise lest you measure the noise. You're not having a useful prediction falling out here, it's not like you can replace line breaks with semi colons and have it run faster on windows. The difference may disappear or reverse if you move things around, run it longer, take turns randomly, and so on.

  • Custom User Avatar

    @BobtheLantern thanks man for bringing more light into it ;p so its a windows thing, good to know. Thanks for the thread guys, we learn something new every day <3 anybody also on leedcode?

  • Custom User Avatar

    I ran the tests on two different machines, one running windows and one running linux. I confirmed that they both compiled to the same byte code on both machines (although with slightly different grouping as seen in b4b's fork above).

    On the windows machine, the solution without the semicolon did run on average about 5% slower than the one with the semicolon, and this was consistent across many tests. On the linux machine, there was no difference between the solutions.

    My best guess is that cpython acts differently enough between the two environments that the grouping of the bytecode causes different memory allocation and fetching behavior on each system, but I'm honestly out of my depth on this topic, so that's all it is - a guess.

  • Custom User Avatar

    Use spoiler flag next time. I've done it for you this time.

  • Custom User Avatar

    when you define t3 you use halving_sum2 but when you print t3 you say it's halving_sum1, if you have to define something more than once that should immediately set off alarm bells about mixing them up.

  • Custom User Avatar

    This comment is hidden because it contains spoiler information about the solution

  • Custom User Avatar

    Read my message again...

  • Custom User Avatar

    did you time it though? when i did back then it had different results..

  • Custom User Avatar

    Be serious guys...
    If you get different results, that means you're doing something wrong.

    Byte codes are strictly identical for both versions, and that is what is running "under the hood".

  • Custom User Avatar

    because the code does not need to change line when executing, less overhead

  • Custom User Avatar

    please let me know if you find the answer

  • Custom User Avatar

    guys why is the semicolon making my code faster in timeit?

  • Custom User Avatar

    Such a beautiful solution. Never thought bitwise operators can be so elegant.