3 ms·
The C program isn't doing the same thing. It overwrites the array of variables each time it scans a line. numpy.loadtxt returns an array of the parsed lines. So
by saretired 12y ago
The C program isn't doing the same thing. It overwrites the array of variables each time it scans a line. numpy.loadtxt returns an array of the parsed lines. So you'd need to do some dynamic allocation to store the parsed values. If the number of fields per input line isn't hardcoded, you'll need to walk sscanf() across each input line while managing input and output buffers (you can call read(), which is much faster, but you'll need to handle partial line reads). numpy.loadtxt has additional capabilities, all of which add per-line overhead. A fully functional clone of loadtxt in C is unlikely to run enormously faster, i.e. enough to justify the programming involved. In any case, this toy exercise isn't testing the speed of fscanf() in a meaningful way.