Click here to Skip to main content
15,920,217 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
Questionhow to add space to a string Pin
Joy Anne13-Jul-06 17:27
Joy Anne13-Jul-06 17:27 
AnswerRe: how to add space to a string Pin
Code_Zombie13-Jul-06 17:36
Code_Zombie13-Jul-06 17:36 
AnswerRe: how to add space to a string Pin
see me13-Jul-06 17:37
see me13-Jul-06 17:37 
AnswerRe: how to add space to a string Pin
KellyR13-Jul-06 18:34
KellyR13-Jul-06 18:34 
Questioncan tab control join project togehter? Pin
mimimimilaw13-Jul-06 17:21
mimimimilaw13-Jul-06 17:21 
AnswerRe: can tab control join project togehter? Pin
see me13-Jul-06 17:44
see me13-Jul-06 17:44 
GeneralRe: can tab control join project togehter? Pin
mimimimilaw13-Jul-06 17:47
mimimimilaw13-Jul-06 17:47 
GeneralRe: can tab control join project togehter? Pin
Abhi Lahare13-Jul-06 18:02
Abhi Lahare13-Jul-06 18:02 
QuestionLimiting # of columns in multi-column listbox Pin
KellyR13-Jul-06 17:15
KellyR13-Jul-06 17:15 
AnswerRe: Limiting # of columns in multi-column listbox [modified] Pin
Abhi Lahare13-Jul-06 17:36
Abhi Lahare13-Jul-06 17:36 
GeneralRe: Limiting # of columns in multi-column listbox Pin
KellyR13-Jul-06 17:44
KellyR13-Jul-06 17:44 
GeneralRe: Limiting # of columns in multi-column listbox Pin
Abhi Lahare13-Jul-06 18:01
Abhi Lahare13-Jul-06 18:01 
GeneralRe: Limiting # of columns in multi-column listbox [modified] Pin
KellyR13-Jul-06 18:10
KellyR13-Jul-06 18:10 
AnswerRe: Limiting # of columns in multi-column listbox Pin
Ryan Binns13-Jul-06 18:34
Ryan Binns13-Jul-06 18:34 
GeneralRe: Limiting # of columns in multi-column listbox Pin
KellyR13-Jul-06 18:36
KellyR13-Jul-06 18:36 
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 

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.