slowness

J

Jason

I have a graphicsPath object filled up with apporximately 2000 points, I am
coping them out into my own point class so I can do some extra operations on
them, its super slow. In C++ this would be pretty fast, not instant, but
would take in the milliseconds, in c# is taking up to 10 seconds. The
operations I am doing are really complicated but blazing fast compared to
just getting the points out of the graphics path. I read that structs ( and
all by value entities ) are thread safe, which if its locking some mutex or
semiphore could account for the bog. Is this what the problem is or could it
be something else?

How can I speed up the process of getting the points out of the
graphicsPath?


heres a litte sample of my point class...just the construction

internal class Point2D
{
#region Member Variables
float m_X;
float m_Y;
#endregion

#region Constructor

public Point2D()
{
m_X = 0;
m_Y = 0;
}

public Point2D( float x, float y )
{
m_X = x;
m_Y = y;
}

public Point2D( Point2D pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

public Point2D( PointF pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

......
 
N

Nicholas Paldino [.NET/C# MVP]

Jason,

It doesn't look like you are using structures though. Also, if you are
creating 2000 instances of these objects, that's going to take some time
(not 10 seconds, but definitely some overhead).

Can you show some code indicating how you are using these class
instances? That would give a better idea.
 
J

Jon Skeet [C# MVP]

Jason said:
I have a graphicsPath object filled up with apporximately 2000 points, I am
coping them out into my own point class so I can do some extra operations on
them, its super slow. In C++ this would be pretty fast, not instant, but
would take in the milliseconds, in c# is taking up to 10 seconds. The
operations I am doing are really complicated but blazing fast compared to
just getting the points out of the graphics path. I read that structs ( and
all by value entities ) are thread safe, which if its locking some mutex or
semiphore could account for the bog. Is this what the problem is or could it
be something else?

There's nothing inherently thread-safe about structs. If you have a
struct as shared data (as a member of a shared reference type object,
for example, or in an array) you have exactly the same problems as you
would with reference types.

You haven't shown how you're actually getting the points out of the
path.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
J

Jason

Heres a sample. Its going to make a number of polygons ( if there are is more than 1 path in the graphics path ) from the graphicss Path, each polygon is just a wrapper for a ArrayList of Point2Ds.


public static ArrayList CreateFromGraphicsPath( GraphicsPath graphicsPath )
{
ArrayList polygonList = new ArrayList();

bool bCloseSubPath = false;
bool bLine = false;
bool bBezier = false;

Polygon polygon = new Polygon();

for ( int i = 1; i < graphicsPath.PathPoints.Length; i++ )
{


GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );

if ( bBezier )
{
//this is a bezier point, solve for linear segments
polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
i+=2;
//re-evaluate point type to see if we need to close
GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );
}
else
{//just a line point ... I assume

polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
if ( bCloseSubPath && bLine )
{
polygon.AddPoint( new Point2D(graphicsPath.PathPoints) );
}

}

if ( bCloseSubPath )
{
polygonList.Add( polygon );
polygon = new Polygon();
}
}


return polygonList;
}

And the top bit of polygon that covers adding/getting points

internal class Polygon
{
ArrayList m_Points = new ArrayList();

int m_CachedIsSimple = -1;

public Polygon( )
{
}


public void AddPoint ( Point2D point )
{
m_Points.Add( point );
}

public Point2D GetPoint ( int index )
{
Debug.Assert ( index >= 0 );
Debug.Assert ( index < GetPointCount() );

return m_Points[index] as Point2D;
//return (Point2D) m_Points[index];
}


Nicholas Paldino said:
Jason,

It doesn't look like you are using structures though. Also, if you are
creating 2000 instances of these objects, that's going to take some time
(not 10 seconds, but definitely some overhead).

Can you show some code indicating how you are using these class
instances? That would give a better idea.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jason said:
I have a graphicsPath object filled up with apporximately 2000 points, I am
coping them out into my own point class so I can do some extra operations
on
them, its super slow. In C++ this would be pretty fast, not instant, but
would take in the milliseconds, in c# is taking up to 10 seconds. The
operations I am doing are really complicated but blazing fast compared to
just getting the points out of the graphics path. I read that structs (
and
all by value entities ) are thread safe, which if its locking some mutex
or
semiphore could account for the bog. Is this what the problem is or could
it
be something else?

How can I speed up the process of getting the points out of the
graphicsPath?


heres a litte sample of my point class...just the construction

internal class Point2D
{
#region Member Variables
float m_X;
float m_Y;
#endregion

#region Constructor

public Point2D()
{
m_X = 0;
m_Y = 0;
}

public Point2D( float x, float y )
{
m_X = x;
m_Y = y;
}

public Point2D( Point2D pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

public Point2D( PointF pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

.....
 
J

Jason

this might be helpful also

private static void GetGraphicsPathTypeInfo( byte pathType, ref bool bCloseSubPath, ref bool bLine, ref bool bBezier )

{

bCloseSubPath = ( pathType & (byte)PathPointType.CloseSubpath ) > 0;

if ( bCloseSubPath )

{//remove CloseSubpath flag since its OR'ed in

pathType = (byte)(pathType & (~(byte)PathPointType.CloseSubpath));

}

bLine = ( pathType == (byte)PathPointType.Line );

bBezier = ( pathType == (byte)PathPointType.Bezier );

// JW - bezier and bezier3 appear to be the same

// bool bBezier3 = ( pathType == (byte)PathPointType.Bezier3 );

}

Heres a sample. Its going to make a number of polygons ( if there are is more than 1 path in the graphics path ) from the graphicss Path, each polygon is just a wrapper for a ArrayList of Point2Ds.


public static ArrayList CreateFromGraphicsPath( GraphicsPath graphicsPath )
{
ArrayList polygonList = new ArrayList();

bool bCloseSubPath = false;
bool bLine = false;
bool bBezier = false;

Polygon polygon = new Polygon();

for ( int i = 1; i < graphicsPath.PathPoints.Length; i++ )
{


GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );

if ( bBezier )
{
//this is a bezier point, solve for linear segments
polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
i+=2;
//re-evaluate point type to see if we need to close
GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );
}
else
{//just a line point ... I assume

polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
if ( bCloseSubPath && bLine )
{
polygon.AddPoint( new Point2D(graphicsPath.PathPoints) );
}

}

if ( bCloseSubPath )
{
polygonList.Add( polygon );
polygon = new Polygon();
}
}


return polygonList;
}

And the top bit of polygon that covers adding/getting points

internal class Polygon
{
ArrayList m_Points = new ArrayList();

int m_CachedIsSimple = -1;

public Polygon( )
{
}


public void AddPoint ( Point2D point )
{
m_Points.Add( point );
}

public Point2D GetPoint ( int index )
{
Debug.Assert ( index >= 0 );
Debug.Assert ( index < GetPointCount() );

return m_Points[index] as Point2D;
//return (Point2D) m_Points[index];
}


Nicholas Paldino said:
Jason,

It doesn't look like you are using structures though. Also, if you are
creating 2000 instances of these objects, that's going to take some time
(not 10 seconds, but definitely some overhead).

Can you show some code indicating how you are using these class
instances? That would give a better idea.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jason said:
I have a graphicsPath object filled up with apporximately 2000 points, I am
coping them out into my own point class so I can do some extra operations
on
them, its super slow. In C++ this would be pretty fast, not instant, but
would take in the milliseconds, in c# is taking up to 10 seconds. The
operations I am doing are really complicated but blazing fast compared to
just getting the points out of the graphics path. I read that structs (
and
all by value entities ) are thread safe, which if its locking some mutex
or
semiphore could account for the bog. Is this what the problem is or could
it
be something else?

How can I speed up the process of getting the points out of the
graphicsPath?


heres a litte sample of my point class...just the construction

internal class Point2D
{
#region Member Variables
float m_X;
float m_Y;
#endregion

#region Constructor

public Point2D()
{
m_X = 0;
m_Y = 0;
}

public Point2D( float x, float y )
{
m_X = x;
m_Y = y;
}

public Point2D( Point2D pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

public Point2D( PointF pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

.....
 
G

Guest

Jason,

Is your C++ code using the GDI+ library as well? Maybe you should post that
too so we can compare them.

Another test you could try is comparing regular C++ to managed C++. If that
causes a slowdown, you know the problem is the "managed-ness" and not the
specific language choice.

-- Matt

Jason said:
Heres a sample. Its going to make a number of polygons ( if there are is more than 1 path in the graphics path ) from the graphicss Path, each polygon is just a wrapper for a ArrayList of Point2Ds.


public static ArrayList CreateFromGraphicsPath( GraphicsPath graphicsPath )
{
ArrayList polygonList = new ArrayList();

bool bCloseSubPath = false;
bool bLine = false;
bool bBezier = false;

Polygon polygon = new Polygon();

for ( int i = 1; i < graphicsPath.PathPoints.Length; i++ )
{


GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );

if ( bBezier )
{
//this is a bezier point, solve for linear segments
polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
i+=2;
//re-evaluate point type to see if we need to close
GetGraphicsPathTypeInfo( (byte)graphicsPath.PathTypes, ref bCloseSubPath, ref bLine, ref bBezier );
}
else
{//just a line point ... I assume

polygon.AddPoint( new Point2D(graphicsPath.PathPoints[i-1]) );
if ( bCloseSubPath && bLine )
{
polygon.AddPoint( new Point2D(graphicsPath.PathPoints) );
}

}

if ( bCloseSubPath )
{
polygonList.Add( polygon );
polygon = new Polygon();
}
}


return polygonList;
}

And the top bit of polygon that covers adding/getting points

internal class Polygon
{
ArrayList m_Points = new ArrayList();

int m_CachedIsSimple = -1;

public Polygon( )
{
}


public void AddPoint ( Point2D point )
{
m_Points.Add( point );
}

public Point2D GetPoint ( int index )
{
Debug.Assert ( index >= 0 );
Debug.Assert ( index < GetPointCount() );

return m_Points[index] as Point2D;
//return (Point2D) m_Points[index];
}


Nicholas Paldino said:
Jason,

It doesn't look like you are using structures though. Also, if you are
creating 2000 instances of these objects, that's going to take some time
(not 10 seconds, but definitely some overhead).

Can you show some code indicating how you are using these class
instances? That would give a better idea.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Jason said:
I have a graphicsPath object filled up with apporximately 2000 points, I am
coping them out into my own point class so I can do some extra operations
on
them, its super slow. In C++ this would be pretty fast, not instant, but
would take in the milliseconds, in c# is taking up to 10 seconds. The
operations I am doing are really complicated but blazing fast compared to
just getting the points out of the graphics path. I read that structs (
and
all by value entities ) are thread safe, which if its locking some mutex
or
semiphore could account for the bog. Is this what the problem is or could
it
be something else?

How can I speed up the process of getting the points out of the
graphicsPath?


heres a litte sample of my point class...just the construction

internal class Point2D
{
#region Member Variables
float m_X;
float m_Y;
#endregion

#region Constructor

public Point2D()
{
m_X = 0;
m_Y = 0;
}

public Point2D( float x, float y )
{
m_X = x;
m_Y = y;
}

public Point2D( Point2D pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

public Point2D( PointF pointToCopy )
{
m_X = pointToCopy.X;
m_Y = pointToCopy.Y;
}

.....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top