Some optemizing questions
Hallo,
I bought a book recommended to me by someone on this forum (Tricks of the game programming gurus) and it has some interesting pages about optimizing some of the functions.
My first question is about using bit shifting instead of multiplication. I was always under the impression that doing bit shifting was a bit faster then using multiplication. So I made a small program to test how much faster it was. To my surprise I found multiplication to have almost the exact same speed as bit shifting. Here is the program I made, with the different speed results on the top.
Code:
// Dev C++ 4.9.9.2
// Multiplication
// First computer
// 4531 first
// 4516 second
// 4516 third
// Second computer, more itterations
// 11485
// 11485
// 11469
// Bit shift
// First computer
// 4500 first
// 4469 second
// 4469 third
// Second computer, more itterations
// 11438
// 11437
// 11563
#include <windows.h>
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
char *buffer = new char[512 * 512 * 4];
cout << "Starting.." << endl;
double startTime = timeGetTime();
for(int i = 0; i < 1000; i++)
{
for (int y = 0; y < 512; y++)
{
for (int x = 0; x < 512; x++)
{
// The two different ways of doing the multiplication,
// comment out one.
//int offset = ((512 * y) + x ) * 4;
//int offset = (((y << 9)) + x) << 2;
}
}
}
double endTime = (timeGetTime() - startTime);
cout << "Ending..." << endl;
cout << endTime << endl;
delete [] buffer;
system("pause");
return 0;
}
Am I doing something wrong, or is << almost the same as * when it comes to speed?
Also, the book talks about how to draw bitmaps to the screen using logical operators, but it does not really go into any details about how to use it. In the short paragraph it mentions that you can use OR to draw the bitmap. The advantage of using it is that I dont have to use an if statement for each pixel. The disadvantage is that you loose some colour and that it only works on 16bit colours. Does anyone know of where I can read up on this as I dont really understand it and I want to use 32bit colours.
Also, is there a way to get rid of the temp variable in this function:
Code:
// Colour is a array of 4 values
void setPixel(BYTE &screenBuffer, BYTE &colour, int x, int y)
{
int offset = (( SCREENWIDTH * y) + x) * 4;
BYTE *temp = screnBuffer + offset;
memcpy(temp, colour, 4);
}
Or im I better of using this:
Code:
// Colour is a array of 4 values
void setPixel(BYTE &screenBuffer, BYTE &colour, int x, int y)
{
int offset = (( SCREENWIDTH * y) + x) * 4;
screenBuffer[offset++] = colour[0]; // R
screenBuffer[offset++] = colour[0]; // G
screenBuffer[offset] = colour[0]; // B
}
Thanks for your time.