[Insight-users] Profiling of Examples/ImageLinearIteratorWithIndex.cxx

Karl Krissian karl at bwh.harvard.edu
Thu Jun 2 18:41:19 EDT 2005


Hi,

I decided to compare the processing time of some simple itk iterator
example with
its equivalent in C.

I think the result can be interesting to ITK community.
I used a ITK version on linux (mobile pentium centrino 1.7GHz)
compiled with profiling and optimization: -pg -O3 and the profiler is
gprof (GNU).

I added the following classes for the experiment:

Code/Common/itkImageLinearIteratorWithIndex2.h
Code/Common/itkImageLinearIteratorWithIndex2.txx

Code/Common/itkImageLinearConstIteratorWithIndex2.h
Code/Common/itkImageLinearConstIteratorWithIndex2.txx

and changed the example:
Examples/Iterators/ImageLinearIteratorWithIndex.cxx

The code is attached to this email.

The new ImageLinearIteratorWithIndex2 could also be called
ImageLinearIteratorWithoutIndex
because it does not update the index during the ++ and -- operations
which speed up
the evolution.

The ImageLinearIteratorWithIndex example does basically a flip of an RGB
image in the X direction.
The idea is to compare the time of this operation using ITK with the
time of the equivalent
operation using standard C programming (directly accessing pointers to
the data).

I created different procedure with some slight changes to compare their
speed:

1. ProcessITK is the original code
2. ProcessITK1 replaces inputIt.Get() by inputIt.Value()
3. ProcessITK2 replaces outputIt.Set( inputIt.Value() )  by
outputIt.Value().Set(inputIt.Value().GetRed(),inputIt.Value().GetGreen(),inputIt.Value().GetBlue())
4. ProcessITK3 is like ProcessITK2 but using the new Iterator
5. ProcessITK4 is like ProcessITK3 but replaces the ++ and -- operations
but IncPos() and DecPos() which are actual ++ and -- on the pointers
6. ProcessPointer does the same operation (without ITK generality) in a
C style.

The results are the following:

1.   17.51 sec
2.     9.94 sec
3.     3.54 sec
4.     1.64 sec
5.     0.81 sec
6.     0.62 sec

The details are in the file 'profile' but in summary:

1 --> 2 : we avoid creating and deleting an RGB value, which saves
approx. 6 sec (FixedArray constructor and destructor)
2 --> 3 : we avoid the operator= of FixedArray (loops over the number of
elements) and we save 6.74 sec
3 --> 4: not updating the index in the iterator decreases the time of ++
and -- operators, GoToEndOfLine() and NextLine() are also faster
4 --> 5: using ++ and -- instead of += m_Jump and -= m_Jump gains 1.1 sec
5 --> 6: there is still some overhead in the iterator, but a small
difference.

Surprisingly, the procedure GoToBegin() takes 0.05 sec and is only
called twice,
and most of its time is spent calling
itk::ImageRegion<3u>::GetNumberOfPixels() const,
which just multiplies the different dimensions and put the result in a
unsigned long (is it a bug of the processor or of the profiler??...).


Anyway, I think this experiment can be instructive, and it shows that
C++ can be as fast as C,
but with a lot of care.
Also some of the generality of itk is lost (like cast from one type to
another), but for specific filters it is probably be worth.

Any comment is welcome,


Karl



-- 
Karl Krissian, PhD
Instructor in Radiology, Harvard Medical School
Laboratory of Mathematics in Imaging, Brigham and Women's Hospital
Tel:617-525-6232, Fax:617-525-6220




-------------- next part --------------
A non-text attachment was scrubbed...
Name: profile.tgz
Type: application/octet-stream
Size: 17126 bytes
Desc: not available
Url : http://public.kitware.com/pipermail/insight-users/attachments/20050602/9713c44b/profile-0001.obj


More information about the Insight-users mailing list