Difference between revisions of "Profiling"

From OTBWiki
Jump to: navigation, search
m (Reverted edits by Ulofyvy (Talk); changed back to last version by Manuel.grizonnet)
(A few more tips from my analyses)
Line 8: Line 8:
 
*Prefer:
 
*Prefer:
 
''const unsigned int size = v.GetSize();
 
''const unsigned int size = v.GetSize();
for (int i=0;i<size;++i) {}''
+
for (unsigned int i=0;i<size;++i) {}''
 +
 
 +
=== Defensive Programming ===
 +
 
 +
* Avoid defensive programming in function called many times. i.e. avoid testing for programming errors. In particular never use ''std::vector<>::at()''
 +
 
 +
* Prefer ''narrow contracts'', and test for possible runtime errors before calling functions that have contracts. For more information, see these [http://luchermitte.github.io/blog/2014/05/24/programmation-par-contrat-un-peu-de-theorie/ posts] (in French).
 +
 
 +
=== Pixel values ===
 +
Pixel values from ''otb::VectorImage'' are actually dynamic vectors. As such avoid to create, copy et destroy pixel values. Instead prefer to minimize the number of pixel objects created (e.g. try to stick to only one instance declared outside a loop), and then assign them new values.
 +
 
 +
This is also true for generic algorithms that may work on scalar as well as vector pixels.
 +
 
 +
Calling ''static_cast<>'' on pixels is not recommended either as this may induce a converting-construction. Prefer the ''new'' ''itk::CastInto<>'' or ''it::MoveInto<>'' functions which will be proposed as a patch.

Revision as of 16:13, 26 May 2015

This page compiles some profiling conclusions and tips:

Method GetSize()

  • Avoid:

for (int i=0;i<v.GetSize();++i) {}

  • Prefer:

const unsigned int size = v.GetSize(); for (unsigned int i=0;i<size;++i) {}

Defensive Programming

  • Avoid defensive programming in function called many times. i.e. avoid testing for programming errors. In particular never use std::vector<>::at()
  • Prefer narrow contracts, and test for possible runtime errors before calling functions that have contracts. For more information, see these posts (in French).

Pixel values

Pixel values from otb::VectorImage are actually dynamic vectors. As such avoid to create, copy et destroy pixel values. Instead prefer to minimize the number of pixel objects created (e.g. try to stick to only one instance declared outside a loop), and then assign them new values.

This is also true for generic algorithms that may work on scalar as well as vector pixels.

Calling static_cast<> on pixels is not recommended either as this may induce a converting-construction. Prefer the new itk::CastInto<> or it::MoveInto<> functions which will be proposed as a patch.