• merc
    link
    fedilink
    arrow-up
    77
    ·
    11 months ago

    I like this bit at the end:

    As a side note, the program is amazingly performant. For small numbers the results are instantaneous and for the large number close to the 2^32 limit the result is still returned in around 10 seconds.

      • merc
        link
        fedilink
        arrow-up
        15
        ·
        11 months ago

        For a long time I’ve been of the opinion that you should only ever optimize for the next sucker colleague who might need to read and edit your code. If you ever optimize for speed, it needs to be done with massive benchmarking / profiling support to ensure that the changes you make are worth it. This is especially true with modern compilers / interpreters that try to use clever techniques to optimize your code either on the fly, or before making the executable.

        • Klear
          link
          fedilink
          arrow-up
          7
          ·
          edit-2
          11 months ago

          The first rule of optimization: Don’t do it
          The second rule of optimization: Don’t do it yet (experts only)

        • Ephera@lemmy.ml
          link
          fedilink
          arrow-up
          1
          ·
          11 months ago

          I’m absolutely on-board …in application code.

          I do feel like it’s good, though, when libraries optimize. Ideally, they don’t have much else to do than one thing really well anyways.

          And with how many libraries modern applications pull in, you do eventually notice whether you’re in the Python ecosystem, where most libraries don’t care, or in the Rust ecosystem, where many libraries definitely overdo it. Because well, they also kind of don’t overdo it, since as a user of the library, you don’t see any of it, except the culmulative performance benefits.

          • merc
            link
            fedilink
            arrow-up
            1
            ·
            11 months ago

            Libraries are also written and maintained by humans.

            It’s fine to optimize if you can truly justify it, but that’s going to be even harder in libraries that are going to be used on multiple different architectures, etc.

    • blusterydayve26@midwest.social
      link
      fedilink
      English
      arrow-up
      9
      arrow-down
      1
      ·
      edit-2
      11 months ago

      I’m still mad he didn’t use the size of the number to tell the system which block to read first. I feel like that would be a great use of division or maybe modulus?

      • merc
        link
        fedilink
        arrow-up
        7
        ·
        11 months ago

        I just like how he used “% 2” in the Python code he used to generate the C++ code.

  • Hotzilla@sopuli.xyz
    link
    fedilink
    arrow-up
    42
    arrow-down
    1
    ·
    11 months ago

    Could be easily made 50% space saving by only iffin all odds and return even on else. Maybe one if before to handle overflow to avoid wrong even if over the last if.

    • bjorney@lemmy.ca
      link
      fedilink
      arrow-up
      18
      ·
      edit-2
      11 months ago

      Yeah but then ALL even numbers would be slow to compute because you would have to chain through every odd before you know that 2 is even.

      Depends on the expected distribution of input values

    • Th4tGuyII@kbin.social
      link
      fedilink
      arrow-up
      26
      ·
      11 months ago

      There’s not a single thing in this universe that cannot be accomplished with enough IF statements… as long as you’ve got infinite time to wait

        • Klear
          link
          fedilink
          arrow-up
          4
          ·
          11 months ago

          …you mean if you’ve got IFinite time to wait?

      • idunnololz@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        11 months ago

        What if there was an unintentional infinite loop in your code. You could be waiting for infinite time only to learn the code had a bug. D:

      • firecat@kbin.social
        link
        fedilink
        arrow-up
        3
        arrow-down
        1
        ·
        11 months ago

        The problem with if is the answer comes from user. There’s no mathematical reason or scientific explanation, only programmer who thinks the answer should include the subject.

        • Th4tGuyII@kbin.social
          link
          fedilink
          arrow-up
          2
          ·
          11 months ago

          True…

          But even on a more metaphorical level, every single thing that has or will happen in this universe, down to even the smallest quantum fluctuations could be encapsulated into IF statements as long as you had enough of them.

  • case_when@feddit.uk
    link
    fedilink
    English
    arrow-up
    30
    ·
    11 months ago

    This is poetry.

    My favourite part is that he uses the modulo operator in his Python script to generate the C code.

  • Great Blue Heron@lemmy.ca
    link
    fedilink
    arrow-up
    13
    arrow-down
    1
    ·
    11 months ago

    I’m not a good reader - I skim most articles and often miss most of the meaning. I read, and enjoyed, every word of that!

  • themusicman@lemmy.world
    link
    fedilink
    arrow-up
    16
    arrow-down
    7
    ·
    11 months ago

    It’s not a trade off between dev time, execution time and memory as the author claims. It’s materially worse for all 3

    • pflanzenregal@lemmy.world
      link
      fedilink
      arrow-up
      20
      ·
      edit-2
      11 months ago

      I think he was being sarcastic, playing with words. Meaning, that you trade in time, runtime and memory and get nothing in return :D so a pretty bad trade haha.

      Of course it’s worse, I mean, that was the point of this blogpost, wasn’t it? :p It’s just a (long) joke.

  • frezik@midwest.social
    link
    fedilink
    arrow-up
    3
    arrow-down
    1
    ·
    11 months ago

    Those are rookie numbers. Professionals came up with the nested logic monstrosity that is the JSON-LD specification:

    https://www.w3.org/TR/json-ld11-api/#context-processing-algorithm

    Looks through the algorithm bits in the various sections. How would you implement that? The answer is invariably by copying the highly nested statements of the spec directly into your language. Maybe there’s a better way, but you’d have to understand all that nested logic first, and you’d be exhausted at that point and just want to move on.