4 ms·
In my opinion the most interesting part - that does not even get addressed in the article - is this: std::fill(p, p + n, 0) : 1.71732 GB/s std::fi
by numlock86 7y ago
In my opinion the most interesting part - that does not even get addressed in the article - is this:
std::fill(p, p + n, 0) : 1.71732 GB/s
std::fill(p, p + n, '\0') : 30.3459 GB/s
memset(p,0,n) : 30.419 GB/s
Ok, memset ist fast, alright. But why the difference between simply setting 0 vs null char/byte? Is this because 0 is actually larger than a single byte?
- EdSchouten 7y agoIt’s likely because std::fill() is a generic function that can be used to fill any list with any kind of value (even class instances). There likely is some template specialization in place, that if it’s called with a ‘char’ list, that it gets translated into a call into libc’s memset(). That’s why the results for the last two are close to identical. Alternatively, there is no template specialization going on, but it’s the case that the compiler detects that this is identical to memset() and converts it accordingly. LLVM/Clang does this through something called the LibCall optimization pass, IIRC.
- deleted 7y ago[deleted]
- wyldfire 7y agoThe linked article goes into more detail. One calls memset and the other doesn't.
- simfoo 7y agoI would actually consider this a bug of the std library implementation. Would love to see a comparison between different implementations (libc++, libstdc++, MSVC)
- simfoo 7y agoIn fact all three major compilers manage to use memset in all cases, see here: https://godbolt.org/z/hVJfPE https://godbolt.org/z/hVJfPE