Pointers and array processing

Greetings and salutations

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...

Here's the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <Windows.h>

using namespace 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.

It just keeps getting less and less clear to me.
Last edited on
Even the books say the first one should be faster.

The book is wrong and if it taught you to use #define for constants and malloc in C++, then you should not use it. Get a proper book here:
http://www.amazon.com/Primer-4th-Stanley-B-Lippman/dp/0201721481

as far as code optimization, I don't think that's the problem here since I have it disabled in the project.

There's absolutely no point measuring performance without optimizations enabled.

This is suppose to be slower since at the
level of machine code the calculation would be
*(arr + (i * 4)) = 0

The x86 architecture allows addressing in the form base+index*X. The assignment is still a single instruction.

See here as well:
http://www.cplusplus.com/forum/general/28019/
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.

Thanks.
Last edited on
Thanks for this pretty nice and informative site.

Well thanks for all your help, and I'll be seeing you around.
please any one can tell what is going wrong with it .... after taking one value it gives run time error ..



#include<iostream>
using namespace std;
int main()
{
cout<<"enter the nu_digit";
int nu_digit;
string **number=new string *[12345];
cin>>nu_digit;
//string *number[100];
int i=0;
while(nu_digit)
{
cout<<"enter the number";
//number[i]=new string;
// string **number=new string *[nu_digit];
cin>>*number[i];
cout<<*number[i];
i++;
cout<<"enter the nu_digit";
cin>>nu_digit;

}
}
Topic archived. No new replies allowed.