Live data from Hacker News

4 billion if statements (2023)

andreasjhkarlsson.github.io

21–30 of 183 posts

Re: 4 billion if statements (2023)

#21
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…

You can do it even even faster by replacing your if statements (works because the ascii values end in the digit they represent):

    if (d & 1)
       printf("odd\n");
    else 
       printf("even\n")

Re: 4 billion if statements (2023)

#22
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…

I'm disappointed, it's not in rust. :-)

Re: 4 billion if statements (2023)

#23

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

It's a wild statement for a few reasons; your observation is one of them.

Re: 4 billion if statements (2023)

#24
> I saw from the SSD was around 800 MB/s (which doesn’t really make sense as that should give execution speeds at 40+ seconds, but computers are magical so who knows what is going on).

If anyone knows what’s actually going on, please do tell.

Re: 4 billion if statements (2023)

#26
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…

probably easier in bash:

    number="$1"
    if [[ "$number" =~ "^(2|4|6|8|10|12|14|16|18|20)$" ]]; then
        echo even
    elif [[ "$number" =~ "^(1|3|5|7|9|11|13|15|17|19)$" ]]; then
        echo odd
    else
        echo Nan
    fi
A bit limited, but you can scale it up

Re: 4 billion if statements (2023)

#27

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

> I decided to use the slowest language on the planet, Python (thanks to the visionary genius of Ross van der Gussom).

given the article, it's fair to assume the author was joking around

that being said, the way the language is used and its ecosystem do contribute to the executable's efficiency. yet, given C's frugality, or the proximity between its instructions and the executed ones, it's not unfair to say that "C is fast"

Re: 4 billion if statements (2023)

#28

> I saw from the SSD was around 800 MB/s (which doesn’t really make sense as that should give execution speeds at 40+ seconds, but computers are magical so who knows what is going on). If anyone knows what’s actually going on, please do tell.

Presumably after the first run much or all of the program is paged into OS memory

Re: 4 billion if statements (2023)

#29
post #19
post #11

Next put them in a tree for faster lookups.

With a tree you'll be limited by the RAM. I advise to use a database.

Map reduce, cluster geo-failover and CDN caching for optimized coldstarts in case you have to bring it up from scratch in a new datacenter. Bio for contact info, hourly billing. I have helped many startups reach their first 100MARR. Buy my audiobook.

Re: 4 billion if statements (2023)

#30

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

Both, usually. A language's semantics can limit how much a compiler can speed up the language. Python, for example, is extremely difficult to make fast due to the fact that almost everything has the semantics of a hashmap lookup. C, in comparison, has relatively little in it that can't be mapped fairly straightforwardly to assembly, and then most of it can be mapped in a more difficult way to faster assembly.
Post reply on HN