If you can't understand the code, how did you know it was a void function. Please stop with the hyperbole, it's not adding anything to the discussion.
Updated code:
import std.range : iota;
import std.array : array;
import std.algorithm;
import std.experimental.ndslice;
import std.datetime;
import std.conv : to;
import std.stdio;
enum testCount = 10_000;
auto f0() {
auto means = 10_000_000.iota
.sliced(1000, 10000)
.transposed
.map!(r => sum(r) / r.length)
.array;
return means;
}
void main() {
auto r = benchmark!(f0)(testCount);
auto f0Result = to!Duration(r[0] / testCount);
f0Result.writeln;
}
Results: Python: 14.1 msec
D: 41 μs
D is 343.9x faster
"Given the same number of lines (or characters), are you sure you can't write equally fast Python code?"IMO program size is an almost meaningless statistic outside of code golf challenges. LOC is not an indicative measure of code readability, usefulness, or organization.
For example, your Fortran code was 11 lines while the D function (with the return) is eight lines.
"I tested the Fortran version on a slightly slower computer (got 14.7 msec on the Python version). Fortran runs at 0.7 μs, i.e. > 55x faster than D, with less code that's more readable to boot."
Just goes to show why Fortran is still used in a lot of scientific areas.
But two things:
1. Fortran is a much simpler language than D or Python and is much harder to do multipurpose work in it (so I'm told from Fortran programmers, I don't know Fortran myself). So when your program needs to do anything other than number crunching, it's normally done in a separate language. Using D you can have everything in one code base.
2. This article was about Numpy and std.ndslice because those are two areas that I know about and Numpy is a very popular library. Bringing up Fortran's speed here is like commenting on how much faster C++ is in a thread about Ruby.
Also, readability is a subjective idea; I believe the D code is more readable than the Fortran you wrote. Different strokes.