Loops with array and pointer, which one is better

Hi all.
My question is the same to the title of this topic, i'm not sure using pointer and array which ones is better (i mean faster)
here is a simple code depicted my ideal.
Thank alot.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
	double* array1 = new double[1000000];
	double res=1.0;
	double in_mode = 2.0;
	/* in here i dont give initial value to array1 because i just concerned about the performance of both algorithm.*/
	// i mean i don't care about the result of them.
	int position = 30;
	// first case, i using pointer
	double* temp = array1[30];
	for (double* pointer = &array1; pointer < &(array1 + 1000000); pointer++){
		res *= (in_mode - *pointer) / (*temp - *pointer);
	}

	// second case, i use array
	for (int i = 0; i < 1000000; i++){
		res *= (in_mode - array1[i]) / (array1[position] - array1[i]);
	}
}
i got something wrong in the source code so i modified that
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main(){
	double* array1 = new double[1000000];
	double res=1.0;
	double in_mode = 2.0;
	// in here i dont give initial value to array1 because i just concerned about the performance of both algorithm.
	// i mean i don't care about the result of them.
	int position = 30;
	// first case, i using pointer
	double* temp = &array1[30];
	for (double* pointer = &array1[0]; pointer < &array1[1000000]; pointer++){
		res *= (in_mode - *pointer) / (*temp - *pointer);
	}

	// second case, i use array
	for (int i = 0; i < 1000000; i++){
		res *= (in_mode - array1[i]) / (array1[position] - array1[i]);
	}
        return 0;
}
Last edited on
closed account (48T7M4Gy)
arrays and pointers are different things so one is not better than the other.
With a good optimizing compiler, you won't see any significant difference.

Compile your program with assembler output and compare the generated instructions each way.

In both cases, the compiler should load the effective address into a register and increment the the register as you iterate.

> i'm not sure using pointer and array which ones is better (i mean faster)

Measure it, on the specific implementation that is used.

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
45
46
47
#include <iostream>
#include <vector>
#include <numeric>
#include <ctime>
#include <algorithm>

struct timer
{
    const std::clock_t start = std::clock() ;
    ~timer()
    {
        const auto end = std::clock() ;
        const auto ms = (end-start) * 1000.0 / CLOCKS_PER_SEC ;
        std::cout << ms << " msecs.\n" << std::flush ;
    }
};

int main()
{
    constexpr std::size_t N = 200'000'000 ;
    std::vector<int> seq(N) ;
    std::iota( std::begin(seq), std::end(seq), 1 ) ;

    {
        std::cout << "classical for loop (position): " ;
        timer t ;
        for( std::size_t i = 0 ; i < N ; ++i ) ++seq[i] ;
    }

    {
        std::cout << " classical for loop (pointer): " ;
        timer t ;
        for( auto ptr = std::begin(seq) ; ptr != std::end(seq) ; ++ptr ) ++*ptr ;
    }

    {
        std::cout << "   range-based loop (pointer): " ;
        timer t ;
        for( int& v : seq ) ++v ;
    }

    {
        std::cout << "          algorithm (pointer): " ;
        timer t ;
        std::transform( std::begin(seq), std::end(seq), std::begin(seq), [] ( int v ) { return v+1 ; } ) ;
    }
}


LLVM, GNU (coliru), -O3: no discernible difference in performance
------------- clang++/libc++ ----------------

classical for loop (position): 180 msecs.
 classical for loop (pointer): 180 msecs.
   range-based loop (pointer): 190 msecs.
          algorithm (pointer): 180 msecs.

------------- g++/libstdc++  ----------------

classical for loop (position): 220 msecs.
 classical for loop (pointer): 210 msecs.
   range-based loop (pointer): 200 msecs.
          algorithm (pointer): 200 msecs.

http://coliru.stacked-crooked.com/a/19f3059558340b78

Microsoft (rextester), -Ox: classical position based for loop and range-based loop appear to be faster
classical for loop (position): 169 msecs.
 classical for loop (pointer): 246 msecs.
   range-based loop (pointer): 167 msecs.
          algorithm (pointer): 580 msecs.

http://rextester.com/UVUN83072
Topic archived. No new replies allowed.