Hello, World!

H. S. Teoh hsteoh at quickfur.ath.cx
Tue Mar 29 17:13:14 UTC 2022


On Tue, Mar 29, 2022 at 04:23:12PM +0000, Petar via Digitalmars-d wrote:
[...]
> Even though some regard `import std;` as an anit-pattern, I think it's
> a nice feature and it's the compiler's job to make it fast (e.g.
> assuming the Phobos source files rarely change, the compiler could
> generate an import cache file to make look ups faster).
[...]

To add more substance to this discussion, I decided to make some real
measurements on the impact of `import std;`.

The following timings were obtained by compiling the same input file 100
times with dmd. The loop was done in the shell, so there's likely to be
some shell overhead, but I'm making the assumption that this is
negligible.

// Empty main() with no imports (base case)
real	0m12.830s
user	0m9.970s
sys	0m2.822s

// Empty main() with `import std;`
real	1m21.429s
user	1m7.603s
sys	0m13.529s

// Empty main() with `import std.stdio;`
real	0m17.745s
user	0m13.888s
sys	0m3.793s

// main() prints "Hello, World!" with `import std.stdio;`
real	0m20.710s
user	0m16.317s
sys	0m4.316s

// main() prints "Hello, World!" with `import std;`
real	1m21.547s
user	1m7.709s
sys	0m13.549s

The impact of `import std;` is quite apparent. It adds about a minute to
the overall compile time, over a more specific `import std.stdio;`.
Invoking `writeln` adds only 3 seconds to the measurement, negligible.

Though keep in mind that these measurements are magnified 100x, so the
actual impact of `import std;` (when compiling only once) is only about
0.8s.

//

I was curious about how this compares with the impact of using some of
the well-known heavyweight template functions like writefln. So here are
some further measurements:

// `import std.stdio;` with `writefln("Hello, %s!", "World");`
real	0m58.014s
user	0m48.912s
sys	0m8.858s

// `import std;` with `writefln("Hello, %s!", "World");`
real	1m33.955s
user	1m19.148s
sys	0m14.460s

Interestingly enough, the impact of `import std;` vs `import std.stdio;`
seems narrower here. The instantiation of writefln added about 30-40
seconds to the measurement in the `import std.stdio;` case, but seems to
have less impact in the `import std;` case.  This suggests that in
non-trivial template-heavy code, the cost of instantiating templates may
quickly overshadow the overhead of `import std;`, which jives with my
own experience in my recent projects (`import std;` is just so darned
convenient I have little motivation for writing specific imports!).


T

-- 
Life is unfair. Ask too much from it, and it may decide you don't deserve what you have now either.


More information about the Digitalmars-d mailing list