views:

187

answers:

6

I have a control that has a grid. Is it more expensive to draw the horizontal and vertical lines that make up the grid each time using the draw line function in the device context class or would it be faster to draw the grid once to a memory device context and then blit it each time to the window dc? Thanks.

+5  A: 

It depends on many things, including how many lines you have, how big the grid is, and what video card the user has.

Your best bet is to just do one of them (probably DrawLine, which my gut feeling says will be both simpler and faster) then test it on a slow machine. If it works and it's fast enough, don't prematurely optimise it.

RichieHindle
+1: For suggestion to try both and record the results - excellent answer :-)
Jon Cage
From the "Great programming quotes" question: "We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil." - Donald Knuth
A. Scagnelli
+1  A: 

A had made such a control and I drew the border for each cell when it was time to update the cell. (It's just an alternative thought)

Nick D
A: 

It also depends on what sort of pen you're using. For simple solid coloured horizontal and vertical lines, IIRC filling rectangles is usually the fastest, but it should be fairly simple to conduct your own benchmark.

Pete Kirkham
+2  A: 

Another option: Create a brush with the grid pattern and just FillRect() the area. Most of the time, this is the fastest way to do it, from my experience..

However, "RichieHindle" is right; there is no need to philosophy about it if you can have the facts by just measuring the speed ;) ..

beef2k
A: 

Short answer: The latter. Blitting to a memory DC once such that it can be used as a bitmap for subsequent re-paint operations is usually the faster approach for rendering.

selbie
A: 

Blitting is a preferred ,as it would copy the image in memory . Copying the image, shall prevent flicker, when drawing it.

Sujay Ghosh