in the following code the first function zeroOutArray1was suppose to be faster, no?? Well I can't understand why it runs slower then the second zeroOutArray2. Even the books say the first one should be faster. I'm using VS2010 and it's compiler, as far as code optimization, I don't think that's the problem here since I have it disabled in the project. Well...
#include <iostream>
#include <Windows.h>
usingnamespace std;
#define SIZE 100000000
void zeroOutArray1(int *p, int length); // Suppose be the faster one
void zeroOutArray2(int arr[], int length); // The slower one with
int main(void)
{
int *myArray = (int*)malloc(SIZE*sizeof(int));
int i, timeElapsed;
cout << "Starting zeroOutArray1" << endl;
timeElapsed = GetTickCount();
zeroOutArray1(myArray, SIZE); // Function call
timeElapsed = GetTickCount() - timeElapsed; // Measuring time
cout << "Finished in " << timeElapsed << endl << endl;
cout << "Starting zeroOutArray2" << endl;
timeElapsed = GetTickCount();
zeroOutArray2(myArray, SIZE); // Function call
timeElapsed = GetTickCount() - timeElapsed; // Measuring time
cout << "Finished in " << timeElapsed << endl;
system("PAUSE");
return 0;
}
void zeroOutArray1(int *p, int length)
{
while (length-- > 0)
*(p++) = 0; // Pointer arithmetic was suppose to be faster, no?
}
void zeroOutArray2(int arr[], int length)
{
for (int i = 0; i < length; i++)
arr[i] = 0; // This is suppose to be slower since at the
// level of machine code the calculation would be
// *(arr + (i * 4)) = 0
}
Thank you for all/any help.
EDIT:
I noticed that changing the function zeroOutArray1 to this:
1 2 3 4 5
void zeroOutArray1(int *p, int length)
{
for (int i = 0; i < length; i++)
*(p++) = 0; // Pointer arithmetic was suppose to be faster, no?
}
makes the race, neck and neck, very close, but still zeroOutArray2 is usually a bit faster still.
Thank you for your answer. As far as my poor programming practice goes (#define and malloc()) it is not the books fault, and thank you for your recommendation about the book, I will look into it after I'm finished with mine first.
As far as my problem is concerned, I now see, it's a bit more complicated then just simple pointer arithmetic and the book seems to be making some simple assumptions here, it would seem. But I am wondering what the problem with #define is here? I know that new should be in place of malloc(), but with #define I do not see why it is so wrong here.