Skip to content(if available)orjump to list(if available)

4 billion if statements (2023)

4 billion if statements (2023)

14 comments

·December 6, 2025

ZeroConcerns

Ah, yes, exactly the pointless diversion I needed for my lunch break. For science: generating a C# switch statement for similar purposes took 7 minutes on similar-ish hardware, but the resulting 99.2GB file could not be opened or compiled ('Stream is too long'), which was slightly disappointing.

Optimization efforts included increasing the internal buffer size of the StreamWriter used to create the source code: this reduced the runtime to around 6 minutes, as well as running a non-debug build, as it was observed that the poor Visual Studio metrics gathering process was contributing significantly to disk activity as well, but that ultimately didn't matter much. So, ehhm, yes, good job on that I guess?

xg15

> 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...

_ache_

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");""")

cowsandmilk

How horridly inefficient, he should have just flipped a Boolean on each iteration.

wiz21c

Gemini took 4 seconds to answer this prompt: "Here is a number 4200020010101. Think deeply about it and tell me if it is not or or not even."

So if you're concerned with privacy issues, you can run the assembly version proposed in the article locally and be well within the same order of performance.

Let's thank the author of the article for providing a decent alternative to Google.

ah, but the license is not that good we can't reproduce his code.

isoprophlex

> Visionary genius Ross van der Gussom

Thanks for making me doubt myself & googling who that guy who made python was again, because surely "van der Gussom" isn't a normal Dutch name. Well played.

orzig

If the author is available for consulting I have this bag of rice I need cooked. Should be around 30,000 grains, each needs about 1mL of water and 2m on the stove. Will pay $10 (2025 dollars)

SiempreViernes

> 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.

Amazing!

wvbdmp

Next put them in a tree for faster lookups.

thewisenerd

discussed 2 years ago,

https://news.ycombinator.com/item?id=38790597

4B If Statements (469 comments)

unwind

Meta: Yeah, this should have a "(2023)" tag in the title. Thanks.

tigranbs

This reminds me of my personal "prime number" grabber research https://github.com/tigranbs/prime-numbers I needed to create the unique graph nodes and assign prime numbers, and to make things efficient, I thought, why not just download the list of known prime numbers instead of generating them one by one. So I did and compiled everything with a single Go binary. Ehh, good old days with a nice feith in making "the best" crappy software out there.

d-lisp

if(n&1)

else

imtringued

You can do it even faster with the if statements:

    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char *argv[])
    {
        if (argc < 2) {
            fprintf(stderr, "Usage: %s <string>\n", argv[0]);
            return 1;
        }

        char *s = argv[1];
        int i;

        /* find the end of the string */
        for (i = 0; s[i] != '\0'; ++i)
            ;

        /* make sure the string wasn't empty */
        if (i == 0) {
            fprintf(stderr, "Error: empty string\n");
            return 1;
        }

        /* last character is at s[i - 1] */
        char d = s[i - 1];

        if (d == '0')
            printf("even\n");
        if (d == '1')
            printf("odd\n");
        if (d == '2')
            printf("even\n");
        if (d == '3')
            printf("odd\n");
        if (d == '4')
            printf("even\n");
        if (d == '5')
            printf("odd\n");
        if (d == '6')
            printf("even\n");
        if (d == '7')
            printf("odd\n");
        if (d == '8')
            printf("even\n");
        if (d == '9')
            printf("odd\n");
    
        return 0;
    }

gcc -std=c11 -Wall -Wextra -O2 -o check_digit check_digit.c

./check_digit 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999