r/ProgrammerHumor • • 6d ago • Bravo 1

Meme pythonSlow

Post image
11k Upvotes

418 comments sorted by

View all comments

38

u/irishcoughy 5d ago

ITT people who write code for a living or as a hobby seemingly unable to grasp that programming languages have use-cases and are rarely if ever universally applicable.

20

u/squabzilla 5d ago

One of my favourite learning experiences was writing C code to read a .txt file, and return the average number of letters per word.

We couldn’t use any libraries except the standard input/output library, so I’m reading stuff character by character, storing it in my own custom-built linked list, make sure there aren’t any memory leaks…

And the next lesson started by showing us how to accomplish the same task with 10 lines of Python code.

And you wanna know which one was faster? Well, as far as my human perception could tell, both of them ran instantly. I mean, sure, technically the compiled code written by the comp sci PHD was a tad faster, but who actually gives a shit?

There’s a reason C++ isn’t used for data analysis. (Or why Python isn’t used for video games for that matter.)

4

u/HumanContinuity 5d ago

Basically the entire python data science and AI/ML ecosystem is just a bunch of C/C++/Rust wrappers.

If you could write them natively in python, you would absolutely see a difference when you throw them even mid sized datasets 

5

u/squabzilla 4d ago

Yes, Python is arguably just an API to interact with efficient C/C++/Rust libraries. And Python does a very good job of that. Which is why data analysis uses Python.

3

u/KevinJRussell 4d ago

Data analysis uses dozens of languages.

3

u/gundam1945 5d ago

I think for the case of data science, the cost of writing something wrong is more costly than the slow speed. And a good thing is everyone have a agreed way of doing the methods, with the public library.

2

u/squabzilla 4d ago

Data science with Python isn’t slow.

Sure, that’s because the public libraries use optimized C/C++/Rust code, but I can use Python as an API to interact with those libraries without ever needing to write a single line of C/C++/Rust code myself. Just like I can write ML code without personally needing to write kernel-level code to use my GPU.

2

u/olorochi 4d ago
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <ctype.h>

typedef unsigned long ulong;

int countWordChars(FILE *fp, ulong *words, ulong *sum)
{
    char buffer[4096];
    bool inWord = false;

    size_t read;
    while ((read = fread(buffer, 1, sizeof(buffer), fp))) {
        for (int i = 0; i < read; ++i) {
            unsigned char c = buffer[i];
            if (isspace(c)) {
                *words += inWord;
                inWord = false;
            } else {
                ++*sum;
                inWord = true;
            }
        }
    }

    *words += inWord;
    return ferror(fp);
}

int main(int argc, char **argv)
{
    ulong words = 0;
    ulong sum = 0;
    for (int i = 1; i < argc; ++i) {
        FILE *fp = fopen(argv[i], "r");
        if (fp == NULL) {
            perror(argv[i]);
            return 1;
        }

        int err = countWordChars(fp, &words, &sum);
        fclose(fp);
        if (err) {
            perror("Read error");
            return 1;
        }
    }

    if (words == 0) {
        puts("NaN");
    } else {
        printf("%f\n", (double)sum / words);
    }
}
#!/bin/python
import sys

words = 0
chars = 0

for filename in sys.argv[1:]:
    with open(filename) as f:
        for line in f:
            for word in line.split():
                words += 1
                chars += len(word)

print(chars / words if words else "NaN")
~ $ du -h yes
862M    yes
~ $ time ./a.out yes
3.000000

real    0m3.555s
user    0m2.748s
sys     0m0.538s
~ $ time ./words.py yes
3.0

real    1m25.753s
user    1m24.416s
sys     0m0.613s

0

u/Le_9k_Redditor 5d ago

To be fair it would be a lot more than a tad faster in C with SIMD, if that was a huge pile of logs you needed to process for example it would be very much worth it to use a compiled language. But of course for tasks where performance doesn't matter then python is fine

1

u/throwaway_194js 5d ago

Probably not the best example considering half of NumPy's selling point is vectorization. Broadly speaking, you use C/C++ when you'll need to perform the same analysis repeatedly on high volumes of data (e.g. a researcher at CERN), or the analysis needs to give you a prediction within a hundred microseconds of receiving the data (e.g. quant work at a trading firm). The rest of the time, the dev/compute time trade-off with tools like python or R is so worthwhile it's not even funny.

1

u/HumanContinuity 5d ago

Numpy is almost entirely wrapped C with a sprinkling of Fortran library links

1

u/throwaway_194js 4d ago

Aye, but that's not the same as writing the data analysis script in C. The comparison is about the effort it takes to do so natively in each language given their respective standard toolkits. Do you think a dev who only knows Python has a good chance of securing a pure C role, simply on the basis that some of the Python features they use happen to be written in C in the back end?

0

u/KevinJRussell 4d ago

How many lines was the C solution? 4?

There’s a reason C++ isn’t used for data analysis. (Or why Python isn’t used for video games for that matter.)

Both of these are wrong.

11

u/spottiesvirus 5d ago

people is also unable to grasp "language efficiency" is way less relevant than what they think it is

in modern CPUs a single RAM lookup consumes 200+ CPU cycles just waiting for the response, let alone a network request querying a disk on a server somewhere (I'm looking at you fragmented microservices)

that kind of architectural optimization is way more important than "I'm using C because JavaScript is less efficient"

6

u/violetvoid513 5d ago

Performance is the least of javascript's sins

2

u/TemporaryFearless482 5d ago

Next you’re going to tell me that the PHP OS was a bad idea.

1

u/fingerling-broccoli 5d ago

Except Python. Python is the 2nd best language for most things

1

u/sessamekesh 17h ago

Use cases? For a living or a hobby? Python and Rust, respectively. 

(I joke)