-
Notifications
You must be signed in to change notification settings - Fork 9
ENH: Add benchmark for iterator loop comparison #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
blowekamp
merged 1 commit into
InsightSoftwareConsortium:master
from
blowekamp:add_iterator_benchmark
Dec 10, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,377 @@ | ||
| /*========================================================================= | ||
| * | ||
| * Copyright NumFOCUS | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0.txt | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| * | ||
| *=========================================================================*/ | ||
|
|
||
| // This Benchmark time the performance of different iteration loop while converting between various vector image and | ||
| // pixel types. | ||
|
|
||
|
|
||
| #include <iostream> | ||
| #include "itkImage.h" | ||
| #include "itkVectorImage.h" | ||
| #include "itkVector.h" | ||
| #include "itkRGBPixel.h" | ||
| #include "itkImageRegionRange.h" | ||
| #include "itkImageScanlineIterator.h" | ||
| #include "itkImageScanlineConstIterator.h" | ||
| #include "itkImageRegionIterator.h" | ||
| #include "itkHighPriorityRealTimeProbesCollector.h" | ||
| #include "PerformanceBenchmarkingUtilities.h" | ||
| #include "itkNumericTraits.h" | ||
| #include <iomanip> | ||
| #include <fstream> | ||
|
|
||
|
|
||
| // Helper function to initialize an image with random values | ||
| template <typename TImage> | ||
| typename TImage::Pointer | ||
| CreateAndInitializeImage(const typename TImage::SizeType & size, unsigned int numberOfComponentsPerPixel = 0) | ||
| { | ||
| auto image = TImage::New(); | ||
| typename TImage::RegionType region{ size }; | ||
| image->SetRegions(region); | ||
| if (numberOfComponentsPerPixel > 0) | ||
| { | ||
| image->SetNumberOfComponentsPerPixel(numberOfComponentsPerPixel); | ||
| } | ||
| image->Allocate(); | ||
|
|
||
| // Initialize with simple pattern (pixel index-based) | ||
| using PixelType = typename TImage::PixelType; | ||
| unsigned int count = 0; | ||
|
|
||
| const unsigned int length = image->GetNumberOfComponentsPerPixel(); | ||
|
|
||
| itk::ImageRegionIterator<TImage> it(image, region); | ||
| for (it.GoToBegin(); !it.IsAtEnd(); ++it) | ||
| { | ||
| PixelType pixel{ it.Get() }; | ||
| for (unsigned int k = 0; k < length; ++k) | ||
| { | ||
| pixel[k] = static_cast<typename itk::NumericTraits<PixelType>::ValueType>(count + k); | ||
| } | ||
| it.Set(pixel); | ||
| ++count; | ||
| } | ||
|
|
||
| return image; | ||
| } | ||
|
|
||
|
|
||
| // Method 1: ImageScanlineIterator approach | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| CopyScanlineIterator(const TInputImage * inputPtr, TOutputImage * outputPtr) | ||
| { | ||
| using InputPixelType = typename TInputImage::PixelType; | ||
| using OutputPixelType = typename TOutputImage::PixelType; | ||
| using ImageScanlineConstIterator = itk::ImageScanlineConstIterator<TInputImage>; | ||
| using ImageScanlineIterator = itk::ImageScanlineIterator<TOutputImage>; | ||
|
|
||
| const typename TOutputImage::RegionType outputRegion = outputPtr->GetRequestedRegion(); | ||
| typename TInputImage::RegionType inputRegion = outputRegion; | ||
|
|
||
| ImageScanlineConstIterator inputIt(inputPtr, inputRegion); | ||
| ImageScanlineIterator outputIt(outputPtr, outputRegion); | ||
|
|
||
| const unsigned int componentsPerPixel = inputPtr->GetNumberOfComponentsPerPixel(); | ||
| while (!inputIt.IsAtEnd()) | ||
| { | ||
| while (!inputIt.IsAtEndOfLine()) | ||
| { | ||
| const InputPixelType & inputPixel = inputIt.Get(); | ||
| OutputPixelType value(outputIt.Get()); | ||
| for (unsigned int k = 0; k < componentsPerPixel; ++k) | ||
| { | ||
| value[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); | ||
| } | ||
| outputIt.Set(value); | ||
|
|
||
| ++inputIt; | ||
| ++outputIt; | ||
| } | ||
| inputIt.NextLine(); | ||
| outputIt.NextLine(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Method 1b: ImageScanlineIterator approach using NumericTraits::GetLength() | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| CopyScanlineIteratorNumericTraits(const TInputImage * inputPtr, TOutputImage * outputPtr) | ||
| { | ||
| using InputPixelType = typename TInputImage::PixelType; | ||
| using OutputPixelType = typename TOutputImage::PixelType; | ||
| using ImageScanlineConstIterator = itk::ImageScanlineConstIterator<TInputImage>; | ||
| using ImageScanlineIterator = itk::ImageScanlineIterator<TOutputImage>; | ||
|
|
||
| const typename TOutputImage::RegionType outputRegion = outputPtr->GetRequestedRegion(); | ||
| typename TInputImage::RegionType inputRegion = outputRegion; | ||
|
|
||
| ImageScanlineConstIterator inputIt(inputPtr, inputRegion); | ||
| ImageScanlineIterator outputIt(outputPtr, outputRegion); | ||
|
|
||
| unsigned int componentsPerPixel = itk::NumericTraits<OutputPixelType>::GetLength(outputIt.Get()); | ||
| while (!inputIt.IsAtEnd()) | ||
| { | ||
| while (!inputIt.IsAtEndOfLine()) | ||
| { | ||
| const InputPixelType & inputPixel = inputIt.Get(); | ||
|
|
||
| OutputPixelType value{ outputIt.Get() }; | ||
| for (unsigned int k = 0; k < componentsPerPixel; ++k) | ||
| { | ||
| value[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); | ||
| } | ||
| outputIt.Set(value); | ||
|
|
||
| ++inputIt; | ||
| ++outputIt; | ||
| } | ||
| inputIt.NextLine(); | ||
| outputIt.NextLine(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Method 2: ImageRegionRange approach | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| CopyImageRegionRange(const TInputImage * inputPtr, TOutputImage * outputPtr) | ||
| { | ||
| using InputPixelType = typename TInputImage::PixelType; | ||
| using OutputPixelType = typename TOutputImage::PixelType; | ||
|
|
||
| const typename TOutputImage::RegionType outputRegion = outputPtr->GetRequestedRegion(); | ||
| typename TInputImage::RegionType inputRegion = outputRegion; | ||
|
|
||
| auto inputRange = itk::ImageRegionRange<const TInputImage>(*inputPtr, inputRegion); | ||
| auto outputRange = itk::ImageRegionRange<TOutputImage>(*outputPtr, outputRegion); | ||
|
|
||
| auto inputIt = inputRange.begin(); | ||
| auto outputIt = outputRange.begin(); | ||
| const auto inputEnd = inputRange.end(); | ||
|
|
||
| const unsigned int componentsPerPixel = inputPtr->GetNumberOfComponentsPerPixel(); | ||
| while (inputIt != inputEnd) | ||
| { | ||
| const InputPixelType & inputPixel = *inputIt; | ||
| OutputPixelType outputPixel{ *outputIt }; | ||
| for (unsigned int k = 0; k < componentsPerPixel; ++k) | ||
| { | ||
| outputPixel[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); | ||
| } | ||
| *outputIt = outputPixel; | ||
|
|
||
| ++inputIt; | ||
| ++outputIt; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Method 2b: ImageRegionRange approach using NumericTraits::GetLength() | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| CopyImageRegionRangeNumericTraits(const TInputImage * inputPtr, TOutputImage * outputPtr) | ||
| { | ||
| using InputPixelType = typename TInputImage::PixelType; | ||
| using OutputPixelType = typename TOutputImage::PixelType; | ||
|
|
||
| const typename TOutputImage::RegionType outputRegion = outputPtr->GetRequestedRegion(); | ||
| typename TInputImage::RegionType inputRegion = outputRegion; | ||
|
|
||
| auto inputRange = itk::ImageRegionRange<const TInputImage>(*inputPtr, inputRegion); | ||
| auto outputRange = itk::ImageRegionRange<TOutputImage>(*outputPtr, outputRegion); | ||
|
|
||
| auto inputIt = inputRange.begin(); | ||
| auto outputIt = outputRange.begin(); | ||
| const auto inputEnd = inputRange.end(); | ||
|
|
||
| const unsigned int componentsPerPixel = itk::NumericTraits<OutputPixelType>::GetLength(*outputIt); | ||
| while (inputIt != inputEnd) | ||
| { | ||
| const InputPixelType & inputPixel = *inputIt; | ||
| OutputPixelType outputPixel{ *outputIt }; | ||
| for (unsigned int k = 0; k < componentsPerPixel; ++k) | ||
| { | ||
| outputPixel[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); | ||
| } | ||
| *outputIt = outputPixel; | ||
| ++inputIt; | ||
| ++outputIt; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Method 2c: ImageRegionRange and a Range-based loop using NumericTraits::GetLength() | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| CopyImageRegionRangeNumericTraitsAsRange(const TInputImage * inputPtr, TOutputImage * outputPtr) | ||
| { | ||
| using InputPixelType = typename TInputImage::PixelType; | ||
| using OutputPixelType = typename TOutputImage::PixelType; | ||
|
|
||
| const typename TOutputImage::RegionType outputRegion = outputPtr->GetRequestedRegion(); | ||
| typename TInputImage::RegionType inputRegion = outputRegion; | ||
|
|
||
| auto outputRange = itk::ImageRegionRange<TOutputImage>(*outputPtr, outputRegion); | ||
| auto outputIt = outputRange.begin(); | ||
|
|
||
| const unsigned int componentsPerPixel = itk::NumericTraits<OutputPixelType>::GetLength(*outputIt); | ||
| for (const InputPixelType & inputPixel : itk::ImageRegionRange<const TInputImage>(*inputPtr, inputRegion)) | ||
| { | ||
| OutputPixelType outputPixel{ *outputIt }; | ||
| for (unsigned int k = 0; k < componentsPerPixel; ++k) | ||
| { | ||
| outputPixel[k] = static_cast<typename OutputPixelType::ValueType>(inputPixel[k]); | ||
| } | ||
| *outputIt = outputPixel; | ||
| ++outputIt; | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Helper function to time a single method | ||
| template <typename TInputImage, typename TOutputImage, typename TCopyFunction> | ||
| void | ||
| TimeMethod(itk::HighPriorityRealTimeProbesCollector & collector, | ||
| const std::string & methodName, | ||
| TCopyFunction copyFunc, | ||
| const TInputImage * inputImage, | ||
| typename TOutputImage::Pointer & outputImage, | ||
| int iterations) | ||
| { | ||
| // Allocate output image | ||
| outputImage = TOutputImage::New(); | ||
| outputImage->SetRegions(inputImage->GetLargestPossibleRegion()); | ||
| outputImage->SetNumberOfComponentsPerPixel(inputImage->GetNumberOfComponentsPerPixel()); | ||
| outputImage->Allocate(); | ||
|
|
||
| // Warm-up run | ||
| copyFunc(inputImage, outputImage.GetPointer()); | ||
|
|
||
| // Timed runs | ||
| for (int ii = 0; ii < iterations; ++ii) | ||
| { | ||
| collector.Start(methodName.c_str()); | ||
| copyFunc(inputImage, outputImage.GetPointer()); | ||
| collector.Stop(methodName.c_str()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| // Performance testing function | ||
| template <typename TInputImage, typename TOutputImage> | ||
| void | ||
| TimeIterationMethods(itk::HighPriorityRealTimeProbesCollector & collector, | ||
| const typename TInputImage::SizeType & size, | ||
| const std::string & description, | ||
| int iterations) | ||
| { | ||
|
|
||
| // Create and initialize input image | ||
| auto inputImage = CreateAndInitializeImage<TInputImage>(size, 3); | ||
|
|
||
| typename TOutputImage::Pointer referenceImage; | ||
| typename TOutputImage::Pointer outputImage; | ||
|
|
||
| // Test Method 1: Scanline Iterator - serves as reference | ||
| TimeMethod<TInputImage, TOutputImage>(collector, | ||
| description + "-Scanline", | ||
| CopyScanlineIterator<TInputImage, TOutputImage>, | ||
| inputImage.GetPointer(), | ||
| outputImage, | ||
| iterations); | ||
|
|
||
| // Test Method 2: ImageRegionRange | ||
| TimeMethod<TInputImage, TOutputImage>(collector, | ||
| description + "-Range", | ||
| CopyImageRegionRange<TInputImage, TOutputImage>, | ||
| inputImage.GetPointer(), | ||
| outputImage, | ||
| iterations); | ||
|
|
||
| // Test Method 1b: Scanline Iterator with NumericTraits | ||
| TimeMethod<TInputImage, TOutputImage>(collector, | ||
| description + "-Scanline NT", | ||
| CopyScanlineIteratorNumericTraits<TInputImage, TOutputImage>, | ||
| inputImage.GetPointer(), | ||
| outputImage, | ||
| iterations); | ||
|
|
||
| // Test Method 2b: ImageRegionRange with NumericTraits | ||
| TimeMethod<TInputImage, TOutputImage>(collector, | ||
| description + "-Range NT", | ||
| CopyImageRegionRangeNumericTraits<TInputImage, TOutputImage>, | ||
| inputImage.GetPointer(), | ||
| outputImage, | ||
| iterations); | ||
|
|
||
| // Test Method 2c: ImageRegionRange with NumericTraits - buggy version | ||
| TimeMethod<TInputImage, TOutputImage>(collector, | ||
| description + "-Range NT AsRange", | ||
| CopyImageRegionRangeNumericTraitsAsRange<TInputImage, TOutputImage>, | ||
| inputImage.GetPointer(), | ||
| outputImage, | ||
| iterations); | ||
| } | ||
|
|
||
|
|
||
| int | ||
| main(int argc, char * argv[]) | ||
| { | ||
| if (argc < 4) | ||
| { | ||
| std::cerr << "Usage: " << std::endl; | ||
| std::cerr << argv[0] << " timingsFile iterations imageSize" << std::endl; | ||
| return EXIT_FAILURE; | ||
| } | ||
| const std::string timingsFileName = ReplaceOccurrence(argv[1], "__DATESTAMP__", PerfDateStamp()); | ||
| const int iterations = std::stoi(argv[2]); | ||
| const int imageSize = std::stoi(argv[3]); | ||
|
|
||
| constexpr unsigned int Dimension = 3; | ||
| const auto size = itk::Size<Dimension>::Filled(imageSize); | ||
|
|
||
| std::ostringstream oss; | ||
| oss << "Image Size: " << size; | ||
| std::string description = oss.str(); | ||
|
|
||
| itk::HighPriorityRealTimeProbesCollector collector; | ||
|
|
||
| // Test 1: Image<Vector> to Image<RGBPixel> | ||
| TimeIterationMethods<itk::Image<itk::Vector<float, 3>, Dimension>, itk::Image<itk::RGBPixel<double>, Dimension>>( | ||
| collector, size, "IVf3->IRGB", iterations); | ||
|
|
||
| // Test 2: VectorImage to Image<Vector> | ||
| TimeIterationMethods<itk::VectorImage<float, Dimension>, itk::Image<itk::Vector<double, 3>, Dimension>>( | ||
| collector, size, "VIf->IVd3", iterations); | ||
|
|
||
| // Test 3: Image<Vector> to VectorImage | ||
| TimeIterationMethods<itk::Image<itk::Vector<float, 3>, Dimension>, itk::VectorImage<double, Dimension>>( | ||
| collector, size, "IVf3->VId", iterations); | ||
|
|
||
| // Test 4: VectorImage to VectorImage | ||
| TimeIterationMethods<itk::VectorImage<float, Dimension>, itk::VectorImage<double, Dimension>>( | ||
| collector, size, "VIf->VId", iterations); | ||
|
|
||
| WriteExpandedReport(timingsFileName, collector, true, true, false); | ||
|
|
||
|
|
||
| return EXIT_SUCCESS; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please place all helper functions (that is basically everything, except for
main) in an unnamednamespace.