Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save N-Dekker/e629feb4f2ebcbc2f085d355d81f0f26 to your computer and use it in GitHub Desktop.
Save N-Dekker/e629feb4f2ebcbc2f085d355d81f0f26 to your computer and use it in GitHub Desktop.
Estimates duration of increasing the capacity of `std::vector<itk::NeighborhoodAllocator<int>>`
/*
Estimates the runtime duration that it takes to increase the capacity of
an `std::vector<itk::NeighborhoodAllocator<int>>`.
With VS2017, the `vector::reserve` in this test appears to run more than 4x faster,
now that `itk::NeighborhoodAllocator` has a move-constructor that is `noexcept`.
Related to the discussion, "Regarding C++11 noexcept" at the ITK forum:
https://discourse.itk.org/t/regarding-c-11-noexcept/1517
Niels Dekker, LKEB, Leiden University Medical Center, 2019
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
*/
#include <itkNeighborhoodAllocator.h>
#include <chrono>
#include <vector>
#include <iostream>
int main()
{
for (int i = 0; i < 5; ++i) // Run the same test a few times!
{
itk::NeighborhoodAllocator<int> neighborhoodAllocator;
neighborhoodAllocator.set_size(1);
neighborhoodAllocator[0] = 42;
std::vector<itk::NeighborhoodAllocator<int>> neighborhoodAllocators(7500000, neighborhoodAllocator);
using namespace std::chrono;
const auto timePointBeforeReserve = high_resolution_clock::now();
// Increase the capacity by one! This should either move or copy the elements of
// the `std::vector` into a new internal data buffer.
neighborhoodAllocators.reserve(neighborhoodAllocators.capacity() + 1);
const auto timePointAfterReserve = high_resolution_clock::now();
const auto durationSeconds =
duration_cast<duration<double>>(timePointAfterReserve - timePointBeforeReserve);
std::cout
<< " Duration: " << durationSeconds.count() << " second(s)"
<< std::endl;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment