Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

11–20 of 183 posts

Re: 4 billion if statements (2023)

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

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

Re: 4 billion if statements (2023)

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

Re: 4 billion if statements (2023)

#14
post #6

if(n&1) else

You can do it even faster with the if statements:

    #include 
    #include 

    int main(int argc, char *argv[])
    {
        if (argc \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

Re: 4 billion if statements (2023)

#15
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?

Re: 4 billion if statements (2023)

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

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

Horridly inefficient. Just unfold the loop.

Re: 4 billion if statements (2023)

#17
> 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?

Re: 4 billion if statements (2023)

#20

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

People often use language as synonym for the whole ecosystem including compiler and linker
Post reply on HN