Results 1 to 6 of 6

Thread: Need a little help increasing the speed of this code...

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2001
    Posts
    23

    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~

  2. #2
    jim mcnamara
    Guest
    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 argcchar *argv[]) { 
        
    HDC hDCint Widthint Heightlong ThisColor;
        
    long a,b,c,d;
        
        
    RGB(248,232,248);
        
    RGB(160,208,248);
        
    cRGB200,224,216);
        
    dRGB(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(hDCxy);        
           
    // 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(hDCxya);
              continue;
            }
            if (
    ThisColor==11842740) {
              
    SetPixelV(hDCxyb);
              continue;
            }
            if (
    ThisColor==10526880) {
              
    SetPixelV(hDCxyc);
              continue;
            }
            if (
    ThisColor==6579300) {
              
    SetPixelV(hDCxyd);
              continue;
            }
          }
        }
        return 
    0


  3. #3
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  4. #4
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Also go to Project Settings-> C/C++ tab and in the optimizations combo box choos maximize speed.
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  5. #5
    DamnedMoose
    Guest
    Thanks jim ^_^

    I'm using Borland's free BCC compiler, so I haven't got any of the special features...

  6. #6
    DamnedMoose
    Guest
    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
  •  



Click Here to Expand Forum to Full Width