3 ms·
Stop writing C code like this author does! C is a language for experts and there are lots of things that are wrong here. Especially when writing benchmark code,
by kccqzy 5y ago
Stop writing C code like this author does! C is a language for experts and there are lots of things that are wrong here. Especially when writing benchmark code, when you do want the compiler to optimize.
> int main()
The easiest way to find someone who's inexperienced in C is to find someone who declares a function that takes no arguments with an empty pair of parentheses. In C but not C++ you need to write "void" inside the parentheses.
> myNum *= i;
Signed integer overflow is undefined behavior. You are lucky if the compiler didn't just replace the whole thing with __builtin_unreachable().
Next the function doesn't use the computed variable. A compiler can just optimize the whole thing out.
The point I'm trying to make is that C is a difficult language to write; especially so when you want to benchmark.
- mhh__ 5y agoAdding unreachable to random places you think the compiler should already know about can yield some perf wins! Compilers are quite chaotic when you get deep in the passes.
- josephg 5y agoAll the C compilers I know of can handle "int main()" just fine. Is there any practical reason to prefer "int main(void)"?
- pjscott 5y agoThe compiler used, Clang 13.0.0, does indeed optimize out everything except for the "return 0;" at the end unless you compile with -O0 to disable optimization. I'm not sure whether the author benchmarked with -O0 or benchmarked a no-op, but either one is probably a mistake.
- moonchild 5y ago> The easiest way to find someone who's inexperienced in C is to find someone who declares a function that takes no arguments with an empty pair of parentheses. In C but not C++ you need to write "void" inside the parentheses. C11§6.7.6.3p14: > An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. That declarator is part of a definition. Even if it were not, the elision of a function's formal parameter list does not matter very much if, like 'main', that function is usually not explicitly called.