Skip to main content
Do fewer calculations per iteration
Source Link
user1118321
  • 2.6k
  • 14
  • 16

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    const float state = board.get_state(i);
    const unsigned char channel = static_cast<unsigned char>(state * 255.0);
    
    // red
    *nextPixel = static_cast<unsigned char>(state * 255);channel;
    nextPixel++;
    
    // green
    *nextPixel = static_cast<unsigned char>(state * 255);channel;
    nextPixel++;
    
    // blue
    *nextPixel = static_cast<unsigned char>(state * 255);channel;
    nextPixel++;
    
    // alpha
    *nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    const float state = board.get_state(i);
    
    // red
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // green
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // blue
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // alpha
    *nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    const float state = board.get_state(i);
    const unsigned char channel = static_cast<unsigned char>(state * 255.0);
    
    // red
    *nextPixel = channel;
    nextPixel++;
    
    // green
    *nextPixel = channel;
    nextPixel++;
    
    // blue
    *nextPixel = channel;
    nextPixel++;
    
    // alpha
    *nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    const float state = board.get_state(i);
    
    // red
    nextPixel*nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // green
    nextPixel*nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // blue
    nextPixel*nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // alpha
    nextPixel*nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    float state = board.get_state(i);
    
    // red
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // green
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // blue
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // alpha
    nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    const float state = board.get_state(i);
    
    // red
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // green
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // blue
    *nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // alpha
    *nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.

Source Link
user1118321
  • 2.6k
  • 14
  • 16

One way to speed it up would be to access the arrays directly rather than doing a monstrous number of indexed look-ups into them when you're accessing the sequentially. It could look something like this:

unsigned char* nextPixel = &pixel_data [ 0 ];
const int boardHeight = board.getWorldHeight();
const int boardWidth = board.getWorldWidth();
const int numPixels = boardHeight * boardWidth;
for (int i = 0; i < numPixels; ++i)
{
    float state = board.get_state(i);
    
    // red
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // green
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // blue
    nextPixel = static_cast<unsigned char>(state * 255);
    nextPixel++;
    
    // alpha
    nextPixel = 255;
    nextPixel++;
}

I also recommend using the appropriate types for your data. An OpenGL texture is not a series of individual unsigned chars. It's a 2 dimensional array of RGBA pixels. You should make a structure for them:

struct RGBAPixel {
    unsigned char red;
    unsigned char green;
    unsigned char blue;
    unsigned char alpha;
};

Then your code wouldn't need the comments I added. But this isn't Code Review, so I'll stop here.