Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

71–80 of 183 posts

Re: 4 billion if statements (2023)

#71
post #10

> Now, this is a time-memory tradeoff, but my time on this earth is limited so I decided to meta-program the if statements using a programmer program in a different programming language. for i in range(2*8): if i % 2 == 0: No comment...

It should have used a flag that is being toggled.

Re: 4 billion if statements (2023)

#72

This could be obviously done with much less code: Just add "if"s for all even number, and at the end just return "odd" if none of the evens matched. 50% less code! Or even simpler: If it's 0, return "even". If not, do a recursive call to n-1, if that equals "even", return "odd", otherwise return "even". But the best way is probably to just use a library. Yes, 500MB of additional dependencies, but then it's a one-line…

But then, even numbers will have the worst possible performance.

Re: 4 billion if statements (2023)

#73

I prefer data-driven programming, so a simple: return odd_or_evenness[n]; works for me, alongside a pretty big array.

With data-driven programming, I would have expected an SQL table containing all the precomputed results. Unless you carelessly add an index, it has the same asymptotic performance!

Re: 4 billion if statements (2023)

#74

I prefer data-driven programming, so a simple: return odd_or_evenness[n]; works for me, alongside a pretty big array.

    const odd_or_evenness = comptime blk: {
        var buf: [16]bool = undefined;
        var is_even = false;
        for (&buf) |*b| {
            b.* = is_even;
            is_even = !is_even;
        }
        break :blk buf;
    };
This looks like a promising strategy.

Re: 4 billion if statements (2023)

#76
post #41

Earlier quoted context omitted.

>Think deeply about it and tell me if it is not or or not even I think I just experienced a segfault

Why do they call it even when you of in the true number of out false odd the number?

Has anyone really been far even as decided to use even go want to do Look more like?

Re: 4 billion if statements (2023)

#77
post #13
post #10

> Now, this is a time-memory tradeoff, but my time on this earth is limited so I decided to meta-program the if statements using a programmer program in a different programming language. for i in range(2*8): if i % 2 == 0: No comment...

Yeah... I come here to talk about that. Should have been for i in range(0, 2**8, 2): print(" if (number == "+str(i)+")") print(" printf(\"even\\n\");") print(" if (number == "+str(i + 1)+")") print(" printf(\"odd\\n\");") or for i in range(0, 2**8, 2): print(f""" if (number == {i}) puts("even"); if (number == {i + 1}) puts("odd");""")

What happens when you try to compute 2**8+1 ?

Re: 4 billion if statements (2023)

#79

> I decided to implement this in the C programming language as it’s by far the fastest language on the planet to this day (thanks to the visionary genius Dennis Richie) Am I lost? Aren't the compiler/linker responsible for fast code, not the language itself?

There are language issues as well. 99% of C programs are valid C++, and if you compile with a C++ compiler instead of a C++ compiler will be slightly faster! C++ ha a stronger type system and so once in a while (very rarely) those C programs compile but give incorrect results since C++ allowed the optimizer to make an assumption that wasn't true. Fortran is often even faster because the language allows for even more assumptions. I don't know where Rust fits in here (Rust is hurt today because the better optimizes are designed for C++ and so don't take advantage of extra assumptions Rust allows - it was designed to allow different assumptions from C++ and likely could be better would a ground up optimizer but that would take a large team a decade+ to write: expensive)

Most of the difference in speed is the optimizer/linker. Assuming a fair competition the difference between Ada, C, C++, D, Fortran, Rust, Zig (and many others I can't think of) is very rarely even 1% in any real world situation. Of course it isn't hard add pessimization to make any language lose, sometimes accidentally, so fair competitions are very hard to find/make.

Then again, the article was clearly sarcastic.

Re: 4 billion if statements (2023)

#80

kind of expected gcc to see right through the 300 gigs of code and compile it down to the tenish instructions.

They disabled optimisations: > Lets compile the code, disabling optimizations with /Od to make sure that the pesky compiler doesn’t interfere with our algorithm

I would have wanted to see them look at the assembly from various optimization levels to see if the compiler really did. Ideally o1 or something wouldn't see through this but would generate better code in other ways. Disabled optimizations often are really stupid about how they code gen.
Post reply on HN