News for hackers. Stuff that matters.
Re:Six times faster than C by DeathArrow on Thursday July 06, 2023 @05:24PM (#36623284)
Shouldn't the compiler be able to do that, too?
Re:Six times faster than C by ModernMech on Thursday July 06, 2023 @05:55PM (#36623649)
Yes, there’s always the “sufficiently smart compiler” that can generate this code. Question is, does that compiler exist?
Re:Six times faster than C by Groxx on Thursday July 06, 2023 @06:01PM (#36623741)
I sure hope so. The semantics are trivially identical, the optimizations should be as well, by default - they should depend on semantics, not syntax. And GCC in another comment under this thread seems to be doing similar or identical optimizations in both cases.

I wholly admit that this implies nothing about all optimizers. But it's a pretty reasonable one to expect.

Re:Six times faster than C by charcircuit on Thursday July 06, 2023 @06:06PM (#36623806)
>does that compiler exist?

and if so are the compile times worth it

Re:Six times faster than C by icsa on Thursday July 06, 2023 @08:07PM (#36625132)
These days, I's put my money on zig cc. I.E. zig cc -Os or zig cc -O3
Re:Six times faster than C by eyegor on Thursday July 06, 2023 @08:21PM (#36625263)
Does the zig compiler have many fancy bits? I was under the impression the c support was a "nothing special" compiler that punted to llvm for optimizations.
Re:Six times faster than C by plorkyeran on Thursday July 06, 2023 @11:01PM (#36626543)
Correct, zig currently does not do any of its own optimizations. It won't necessarily have identical results to clang because it'll generate equivalent but not identical IR and the optimizer passes may happen to do something different as a result, but it's not going to be consistently different.
Re:Six times faster than C by thomasahle on Thursday July 06, 2023 @06:21PM (#36624026)
I tried https://godbolt.org/, and neither Clang nor GCC trunk give the same code for the two programs.

Pretty shocking for such a simple program.

Re:Six times faster than C by AnotherGoodName on Thursday July 06, 2023 @07:05PM (#36624525)
It's not true in general.

In general branching code is faster than branchless code and there's many many places that will demonstrate this with a quick Google. You know how many cycles a correctly predicted branch takes? 0.

On the other hand branchless code has to wait for each calculation to reach a certain stage in the pipeline since the thing to be output is dependent on the result. The CPU will have a whole lot of halts.

So why is this faster? Because the input is literally random(). The branch predictor will be wrong. This isn't normal though. The compiler is creating code that will be faster in most normal use cases.

It's an artificial benchmark that works against the output the compiler produces.

Re:Six times faster than C by rendaw on Friday July 07, 2023 @01:41AM (#36627650)
That's what profile guided optimizers are for though, right?