Menu

#28 Improving performance Rows/Columns

Evaluate
open
nobody
General (11)
6
2008-01-15
2008-01-15
No

Here a suggestion on how to improve performance:

---------------------------------------
First off all retreiving the row and column indices is very time
consuming - about 9% each in my app. The reason is, that IndexOf in
List<> has to iterate over the content to get the index back.

If you add some little overhead and use a Dictionary<RowInfo,int>, you
can benifit from the hashtable the Dictionary uses for keys. The index
access would be multiple times faster. (See attachment)

Another time consuming hot spot, i found while profiling, is
Range.Normalize(). I rewrote it to:

private void Normalize()
{
if ((m_Start.Row <= m_End.Row) && (m_Start.Column <= m_End.Column))
{
//nothing to do
return;
} //~ if
else
{
Position l_min = Position.Min( m_Start, m_End );
Position l_max = Position.Max( m_Start, m_End );
m_Start = l_min;
m_End = l_max;
} //~ else
} //~ method (void) Normalize

But after checking the references to that method, i noticed that you
are instantiating mostly four instances of Position. The first pair by
the constructor and after that, the second pair by Normalize().

After rewriting Position.Min and Position.Max to

public static Position Min(Position p_Position1, Position p_Position2)
{
return new Position(Math.Min(p_Position1.Row, p_Position2.Row), Math.Min(p_Position1.Column, p_Position2.Column));
} //~ static method (Position) Min

public static Position Max(Position p_Position1, Position p_Position2)
{
return new Position(Math.Max(p_Position1.Row, p_Position2.Row), Math.Max(p_Position1.Column, p_Position2.Column));
} //~ static method (Position) Max

public static Position Max( int p_StartRow, int p_StartCol, int p_EndRow, int p_EndCol )
{
return new Position( Math.Max( p_StartRow,p_EndRow ), Math.Max( p_StartCol, p_EndCol ) );
} //~ static method (Position) Max

public static Position Min( int p_StartRow, int p_StartCol, int p_EndRow, int p_EndCol )
{
return new Position( Math.Min( p_StartRow, p_EndRow ), Math.Min( p_StartCol, p_EndCol ) );
} //~ static method (Position) Min

and the constructors of Range to

public Range(int p_StartRow, int p_StartCol, int p_EndRow, int p_EndCol)
{
m_Start = Position.Min( p_StartRow, p_StartCol, p_EndRow, p_EndCol );
m_End = Position.Max( p_StartRow, p_StartCol, p_EndRow, p_EndCol );
} //~ constructor (void) Range

private Range(Position p_Start, Position p_End, bool p_bCheck)
{

if (p_bCheck)
{
m_Start = Position.Min( p_Start, p_End );
m_End = Position.Max( p_Start, p_End );
} //~ if
else
{
m_Start = p_Start;
m_End = p_End;
} //~ else
} //~ constructor (void) Range

there is no need to use Normalize() anymore. The normalization is done
by Min and Max without the overhead.

Best regards,
Stephan
---------------------------------------

Discussion

  • Davide Icardi

    Davide Icardi - 2008-01-15

    Changes

     
  • Davide Icardi

    Davide Icardi - 2008-01-15
    • labels: 897609 --> General
    • milestone: 643823 --> Evaluate
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.