Hacker News new | ask | show | jobs
by pron 384 days ago
> But it turns out that a C compiler will give you the "perfect" code directly while the comptime Zig version is fairly complicated.

In this case both would (or could) give the "perfect" code without any explicit comptime programming.

> I just realized at some point that it isn't actually all that useful.

Except, again, C code often uses macros, which is a more cumbersome mechanism than comptime (and possibly less powerful; see, e.g. how Zig implements printf).

I agree that comptime isn't necessarily very useful for micro optimisation, but that's not what it's for. Being able to shift computations in time is usedful for more "algorithmic" macro optimisations, e.g. parsing things at compile time or generating de/serialization code.

1 comments

Of course, a compiler could possibly also optimize the Zig code perfectly. The point is that the blogger did not understand it and instead created an overly complex solution which is not actually needed. Most C code I write or review does not use a lot of macros, and where they are used it seems perfectly fine to me.