Thread: Optimization challenge

  1. #1
    Registered User
    Join Date
    May 2015
    Posts
    65

    Optimization challenge

    I've been wanting to do learn some SDL, but as I never did like to play the same game as all the other kids, I find it both boring and confusing, thus I've setup the minimum amount of code I need, and what's left is simply to come up with some interesting algorithms to manipulate all the pixels on an individual basis.

    I goes without saying that this can quite quickly become quite the task, thus I will eventually look into the "correct" way of doing things, but for now I'm simply interested in the hypothetical question that I'm now going to present.

    Code:
    #include <stdint.h>
    
    const HEIGHT = 1080;
    const WIDTH = 1920;
    
    static uint8_t pixmap[ HEIGHT ][ WIDTH ];
    // assume that pixmap has been filled with random 8 bit values.
    
    void Shade()
    {
        int count;
        for(int y = HEIGHT; y < HEIGHT + HEIGHT; y++)
        {
            for(int x = WIDTH; x < WIDTH + WIDTH; x++)
            {
                count = 8;
                if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                if( pixmap[ ( y - 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
                if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                if( pixmap[ y - HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                if( pixmap[ y - HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                if( pixmap[ ( y + 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
                if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                pixmap[ y - HEIGHT ][ x - WIDTH ] = 0xff >> count;
            }
        }
    }
    The above code, which is for now just an example, designed to illustrate how a relatively simple task can become quite the burden, should do nothing more than count the non zero neighbors of each individual pixel, and from that assign a values to said pixel.

    I've had to jump through a few hoop to avoid getting out of bounds, which I'm sure there must be a better way of doing, and it is of course quite obvious that this "algorithm" if you wish, is very resource intensive.

    So, is there anything I haven't thought of, which can be done to optimize this?



    Best regards

    edit:
    Just tested it for the fun of it, and there is a problem which I cannot figure out. Running the function more than once should yield the same result, but it does not.
    Last edited by Zacariaz; 06-02-2015 at 10:47 AM.

  2. #2
    Registered User
    Join Date
    May 2015
    Posts
    65
    ran out of editing time...

    Anyway, figure out the problem, and here's a slight update to the funtion:

    Code:
    void Shade(){
        int count;
        for(int y = HEIGHT; y < HEIGHT + HEIGHT; y++)
        {
            for(int x = WIDTH; x < WIDTH + WIDTH; x++)
            {
                if(pixmap[ y - HEIGHT ][x - WIDTH])
                {
                    count = 8;
                    if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                    if( pixmap[ ( y - 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
                    if( pixmap[ ( y - 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                    if( pixmap[ y - HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                    if( pixmap[ y - HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                    if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x - 1 ) % WIDTH ] ) count--;
                    if( pixmap[ ( y + 1 ) % HEIGHT ][ x - WIDTH ] ) count--;
                    if( pixmap[ ( y + 1 ) % HEIGHT ][ ( x + 1 ) % WIDTH ] ) count--;
                    pixmap[ y - HEIGHT ][ x - WIDTH ] = 0xff >> count;
                }
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC 4.2.0 SRA optimization
    By brewbuck in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 08-16-2007, 08:47 AM
  2. optimization
    By strickey in forum C++ Programming
    Replies: 7
    Last Post: 04-19-2007, 05:28 AM
  3. Optimization
    By lach in forum C Programming
    Replies: 4
    Last Post: 03-18-2006, 12:08 PM
  4. optimization
    By krithi in forum C Programming
    Replies: 9
    Last Post: 01-19-2003, 10:52 AM
  5. IDE optimization
    By Traveller in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-04-2002, 02:01 AM