Click here to Skip to main content
15,891,951 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionWM_TIMER Pin
thathvamsi13-Jul-06 16:48
thathvamsi13-Jul-06 16:48 
AnswerRe: WM_TIMER Pin
see me13-Jul-06 17:33
see me13-Jul-06 17:33 
Questionanti-debug product for MFC? Pin
darbien siamak13-Jul-06 16:16
darbien siamak13-Jul-06 16:16 
AnswerRe: anti-debug product for MFC? Pin
Phil.Benson13-Jul-06 21:28
professionalPhil.Benson13-Jul-06 21:28 
QuestionFilling a square? Pin
Lord Kixdemp13-Jul-06 12:49
Lord Kixdemp13-Jul-06 12:49 
AnswerRe: Filling a square? Pin
earl13-Jul-06 13:22
earl13-Jul-06 13:22 
GeneralRe: Filling a square? Pin
Lord Kixdemp13-Jul-06 16:10
Lord Kixdemp13-Jul-06 16:10 
GeneralRe: Filling a square? Pin
Rilhas17-Jul-06 12:47
Rilhas17-Jul-06 12:47 
I assume the representation of the image in memory is sequential X and stepped Y. This would mean that pixel X=0/Y=0 is followed in memory by pixel X=1/Y=0, then X=2/Y=0, etc.

With this memory representation of the image your formula would be correct. If your image is 100 pixels wide then the pixel X=17/Y=35 would be located in memory array cell 35*100+17.

However, you should note that your code could have several optimizations, applicable in any language. The most important of them is that all modern processors use cache machanisms, which are exploited to their best if memory accesses are sequential.

This means that your outter loop should be the Y loop, and the inner loop should be the X loop. Something like this:

for (int cy = loc.y; cy < loc.y+loc.h; cy++) {
for (int cx = loc.x; cx < loc.x+loc.w; cx++) {
this->screen[cy*loc.w+cx].Attributes = color;
} // next x
} // next y

The loop order is simply reversed. With this arrangement the access the processor needs to the main memory is optimized, because the X loop accesses the memory sequentially (the pixels are arranged so that pixel X+1 follows pixel X, and the cache can take advantage of this). This can lead to typical improvements of 10x or more (of course, depending on the overall performance of the language). If the Y loop is the inner loop, then no memory access is sequential, since (from the cache point of view) the memory will be accessed in (potentially) large leaps and never sequentially. However, with large caches and small images, the cache could hold all the image square and still behave optimally (the cache would be helping the performance of your code, and your code would not be helping the cache at all).

Another simple optimization would be something like:

int y0=loc.y;
int y1=loc.y+loc.h;
int x0=loc.x;
int x1=loc.x+loc.w;
int pixel_index;
for (int cy = y0; cy < y1; cy++) {
pixel_index=cy*loc.w+cx;
for (int cx = x0; cx < x1; cx++) {
this->screen[pixel_index].Attributes = color;
pixel_index=pixel_index+1;
} // next x
} // next y

The advantages of the previous code are several:

1) Avoid an addition with each iteration of the outter y loop (y1 is precomputted).

2) Avoid an addition with each iteration of the inner x loop (x1 is precomputted).

3) Avoid one multiplication for each pixel. Since the pixel_index is computed at the start of each loop, we take advantage from the fact that adjacent X pixels correspond to adjacent addresses in memory. So, we just increment pixel_index, avoiding the multiplication for each X loop. This could be further inproved if we precomputed how much pixel_index would have to be incremented at the end of each X loop to end up in the first X of the next line, but this typically doesn't improve performace much.

The optimizations 1) and 2) above increase the clarity of the code, but are typically not very significant. Optimization 3) may be much more significant, but it reduces clarity. In any case, these optimizations are noticeable more and more as images become larger and larger.

I hope that you find these very easy to implement hints helpful.

Rilhas
GeneralRe: Filling a square? Pin
Lord Kixdemp18-Jul-06 12:33
Lord Kixdemp18-Jul-06 12:33 
QuestionSubclass main Winamp window from plugin Pin
u0m313-Jul-06 12:13
u0m313-Jul-06 12:13 
AnswerRe: Subclass main Winamp window from plugin Pin
Justin Tay13-Jul-06 17:47
Justin Tay13-Jul-06 17:47 
GeneralRe: Subclass main Winamp window from plugin Pin
u0m315-Jul-06 5:25
u0m315-Jul-06 5:25 
GeneralRe: Subclass main Winamp window from plugin Pin
u0m318-Jul-06 2:15
u0m318-Jul-06 2:15 
GeneralRe: Subclass main Winamp window from plugin Pin
Justin Tay18-Jul-06 3:48
Justin Tay18-Jul-06 3:48 
QuestionCan I still use DLLs ? Pin
vcpp_cgr13-Jul-06 11:49
vcpp_cgr13-Jul-06 11:49 
AnswerRe: Can I still use DLLs ? Pin
Jun Du13-Jul-06 13:34
Jun Du13-Jul-06 13:34 
QuestionHBITMAP from clipboard Pin
Luksky13-Jul-06 11:43
Luksky13-Jul-06 11:43 
AnswerRe: HBITMAP from clipboard Pin
Chris Losinger13-Jul-06 11:52
professionalChris Losinger13-Jul-06 11:52 
GeneralRe: HBITMAP from clipboard Pin
Luksky14-Jul-06 0:18
Luksky14-Jul-06 0:18 
GeneralRe: HBITMAP from clipboard Pin
Luksky15-Jul-06 5:21
Luksky15-Jul-06 5:21 
QuestionError: LNK2019: Unresolved external symbol... Pin
jon-8013-Jul-06 10:42
professionaljon-8013-Jul-06 10:42 
AnswerRe: Error: LNK2019: Unresolved external symbol... Pin
Chris Losinger13-Jul-06 11:52
professionalChris Losinger13-Jul-06 11:52 
GeneralRe: Error: LNK2019: Unresolved external symbol... Pin
jon-8014-Jul-06 6:15
professionaljon-8014-Jul-06 6:15 
GeneralRe: Error: LNK2019: Unresolved external symbol... Pin
Rilhas17-Jul-06 12:54
Rilhas17-Jul-06 12:54 
QuestionC++ Exception Programming Pin
pgav13-Jul-06 9:50
pgav13-Jul-06 9:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.