It was an unpleasant surprise to me, to read here at the forum (December 2017) that noexcept
might sometimes harm the performance. As noted by @matt.mccormick at warning: dynamic exception specifications are deprecated in C++11
Now I’m just posted this question to Microsoft’s Developer Community forum: C++11 noexcept implementation still a performance loss with VS2017? Hope that Visual C++ is doing better by now…
I do have some good news, already I just profiled the following code on Visual Studio 2017 (15.9.5), Release:
std::vector<itk::NeighborhoodAllocator<int>> neighborhoodAllocators;
itk::NeighborhoodAllocator<int> neighborhoodAllocator;
neighborhoodAllocator.set_size(1);
neighborhoodAllocator[0] = 42;
neighborhoodAllocators.assign(7500000, neighborhoodAllocator);
neighborhoodAllocators.reserve(neighborhoodAllocators.capacity() + 1);
I observed that the last line of code, calling std::vector::reserve
, runs more than four times faster now that itk::NeighborhoodAllocator
has a ITK_NOEXCEPT
move-constructor:
itk::NeighborhoodAllocator
move-constructor without ITK_NOEXCEPT
: ~0.6 second
itk::NeighborhoodAllocator
move-constructor with ITK_NOEXCEPT
: ~0.13 second
The ITK_NOEXCEPT
itk::NeighborhoodAllocator
move-constructor that I proposed was merged onto the ITK git master branch by @phcerdan, earlier this week:
So when a class has a manually implemented non-throwing move-constructor that is faster than its copy-constructor, the noexcept
specifier is certainly beneficial.