4 ms·
It is a very bad blog post. Pushing C++ dev to make premature C-style type of optimization is counter-productive... And in this case it is even wrong microben
by adev_ 7y ago
It is a very bad blog post.
Pushing C++ dev to make premature C-style type of optimization is counter-productive... And in this case it is even wrong microbenchmarking.
A simple switch to "-O3 -march=native" flag switch in the makefile to enable vectorization and a proper use of '\0' inverse the result make std::fill() globally faster than memset.
```
page count: 262144, volume:1024 MB
std::fill(p, p + n, '\0') : 25.9191 GB/s
memset(p,0,n) : 25.3283 GB/s
std::fill(p, p + n, '\0') : 25.6167 GB/s
memset(p,0,n) : 25.1646 GB/s
std::fill(p, p + n, '\0') : 25.8355 GB/s
memset(p,0,n) : 25.6276 GB/s
```
It would have been much more productive to describe why std::fill with '0' is slower than with '\0' ( integer vs char )
- adrianN 7y agoClang produces exactly the same code for both: https://godbolt.org/z/C7UTqN https://godbolt.org/z/C7UTqN and at -O3 gcc produces almost the same code for both (not at O2 though)
- adev_ 7y agoThis is also mis-interpretation of micro-benchmark and illustrate pretty well that benchmarking this kind of thing is difficult. If you analyze the ASM generated by the benchmark he proposed, you see something very different of what the godbolt "simple" compilation of micro-isolated function give you. - Most call to "fill" are generally transparently replace by a "memset" call directly by the compiler in high level of optimization when done in a normal glibc/GCC linux configuration. - It can even being removed entirely sometimes and this is also a problem ( https://www.usenix.org/system/files/conference/usenixsecurity17/sec17-yang.pdf https://www.usenix.org/system/files/conference/usenixsecurit... ) - This does not seem to occur on godbolt for ...reasons. https://godbolt.org/z/RGEXH8 https://godbolt.org/z/RGEXH8 - Enable vectorization + some compiler flags can in some case make the memset call replaced by a bunch of SIMD code depending of your compiler / compiler options and/or cpu. - The intel compiler is (like usual) much more aggressive and replace everything by their home-made memset implementation https://godbolt.org/z/7RAYGc https://godbolt.org/z/7RAYGc
- gpderetta 7y ago> It is a very bad blog post. [...] > It would have been much more productive to describe why std::fill with '0' is slower than with '\0' ( integer vs char ) Lemire actually links to BeOnRope post that fully explains the reason. He probably felt that repeating the explanation didn't add anything.