开发者

WPF canvas performance- children.add called many times

开发者 https://www.devze.com 2023-02-07 08:38 出处:网络
I\'m drawing a lot of lines on a long canvas (think stripchart) and have it tuned fairly well for performance, using the low-level geometry classes and freezing them, etc. This improved performance dr

I'm drawing a lot of lines on a long canvas (think stripchart) and have it tuned fairly well for performance, using the low-level geometry classes and freezing them, etc. This improved performance dramatically, but it still takes a few seconds to load a few thousan开发者_StackOverflow社区d items into the canvas. I ran a performance analysis on the application, and it looks like a big percentage of the time is taken by each call to canvas.children.add(). I've read that this should be a lightweight call, and since I'm calling it numerous times in one method, it shouldn't be trying to do anything heavy inbetween... Could there possibly be any other reason this might be taking so much time? And any way I might speed it up?

The performance is not terrible, but I fear it could become more of a problem later when I need to deal with larger sets of data.

Just for reference, it looks like it is called 1400 times in this sample, and it taking almost 3 seconds of CPU time on a modern/fast laptop.

The canvas is contained in a hierachy of other controls though, so I'm curious if they might be contributing to this.

Extra note: I'm also not setting a specific height on the canvas, as it is set to fill the grid parent container. Could this be a source of the problems?


Main problem is that Children.Add is always a slow operation, even if you use StreamGeometry objects. I faced the same problem recently and concluded the following: If you put a bunch of objects into a new canvas and nest it into the main canvas, the performance of the addition operation will be increased dramatically. So, instead of adding 1400 elements, put 200 elements in 7 canvases and add those 7 canvases to the main canvas. Since all objects now belong to different canvases, you will need to adjust your app a bit, but this would be a less drastical solution than moving to an alternative solution like DrawingVisual


Just to add about the hierarchy of controls the canvas is within, and the height of the canvas:

the Canvas always takes as much space as its given, and no matter what children u add to it - it NEVER triggers a new Measuer/Arrange passes on its parents. therefor whatever u do inside a canvas could never affect the visual tree it is contained in. To sum it up - the problem cannot come from there, and the suggestion about the StreamGeomatry is exactly right- this is what causing u the performance issues, and switching to streamgeormatry would solve it.


I would suggest that you draw your shapes directly into an image instead of adding them as children. Rendering children has a HUGE overhead (as you can see).

There's a similar question with a reference to some helpful articles:

How to draw line of ten thousands of points with WPF within 0.5 second?

0

精彩评论

暂无评论...
验证码 换一张
取 消