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.
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.)
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.
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.
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.
#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
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
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.
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?
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"
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.