|
-
Sep 14th, 2001, 10:36 PM
#1
Thread Starter
Junior Member
Need a little help increasing the speed of this code...
Hey, I have some code, which is quite slow - so would anybody out there be able to optimize it for me??
Code:
#include <windows.h>
int main(int argc, char *argv[]) {
HDC hDC; int Width; int Height; long ThisColor;
hDC = (HDC)atoi(argv[1]);
Width = atoi(argv[2]);
Height = atoi(argv[3]);
for(int x=0;x<Width;x++)
{
for(int y=0;y<Height;y++)
{
ThisColor = GetPixel(hDC, x, y);
if (ThisColor==16579836) {
SetPixelV(hDC, x, y, RGB(248,232,248));
}
if (ThisColor==11842740) {
SetPixelV(hDC, x, y, RGB(160,208,248));
}
if (ThisColor==10526880) {
SetPixelV(hDC, x, y, RGB(200,224,216));
}
if (ThisColor==6579300) {
SetPixelV(hDC, x, y, RGB(24,16,16));
}
}
}
return 0;
}
Thanks~
-
Sep 15th, 2001, 04:41 AM
#2
Avoid compares that can't run - use continue
Make the RGB calculation once, not every time.
Use & instead of if( a==b), it's faster
& with || gives you more speed becasue the code stops the compares as soon as it get a true.
This will probably give you about 10% more speed.
You need to learn about profiling.
I broke the & || block up so I could read it - it's 3:00am where I am. Be sure you check my copying your numbers.
If this were gnu c++ you could use register variables, which speeds things up, too.
Also - do you know how to use graphics in memory? You can BiBlt the whole screen into memory, then step thru the pixels, Bitblt the whole thing back again. This would REALLY provide some speed. Working directly on the screen itself is at least 10 times slower.
PHP Code:
#include <windows.h>
int main(int argc, char *argv[]) {
HDC hDC; int Width; int Height; long ThisColor;
long a,b,c,d;
a = RGB(248,232,248);
b = RGB(160,208,248);
c= RGB( 200,224,216);
d= RGB(24,16,16);
hDC = (HDC)atoi(argv[1]);
Width = atoi(argv[2]);
Height = atoi(argv[3]);
for(int x=0;x<Width;x++)
{
for(int y=0;y<Height;y++)
{
ThisColor = GetPixel(hDC, x, y);
// order the colors by most common first
if(
!(ThisColor & 16579836 ||
ThisColor & 11842740 ||
ThisColor & 10526880 ||
ThisColor & 6579300)
)
continue;
// put the color values in the order of the most common color first
if (ThisColor==16579836) {
SetPixelV(hDC, x, y, a);
continue;
}
if (ThisColor==11842740) {
SetPixelV(hDC, x, y, b);
continue;
}
if (ThisColor==10526880) {
SetPixelV(hDC, x, y, c);
continue;
}
if (ThisColor==6579300) {
SetPixelV(hDC, x, y, d);
continue;
}
}
}
return 0;
}
-
Sep 15th, 2001, 06:29 AM
#3
Monday Morning Lunatic
Also, if you're using Visual C++, make sure to choose Release build.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 15th, 2001, 06:46 AM
#4
Frenzied Member
Also go to Project Settings-> C/C++ tab and in the optimizations combo box choos maximize speed.
-
Sep 15th, 2001, 06:13 PM
#5
Thanks jim ^_^
I'm using Borland's free BCC compiler, so I haven't got any of the special features...
-
Sep 15th, 2001, 06:15 PM
#6
btw, I am superbatman, for some reason I got logged out, when I came back, I couldn't remember my password, so I had it e-mailed to me, and got this one emailed -_- go figure lol
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|