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.
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main(){
double* array1 = newdouble[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]);
}
}
#include <iostream>
#include <string>
#include <sstream>
usingnamespace std;
int main(){
double* array1 = newdouble[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;
}