jeudi 13 août 2015

Is there any performance difference between accessing a hardcode array and a run time initialization array?

For example, I want to create a square root table using array SQRT[i] to optimize a game, but I don't know if there is performance difference between the following initialization when accessing the value of SQRT[i]:

  1. Hardcode array

    int SQRT[]={0,1,1,1,2,2,2,2,2,3,3,.......255,255,255}
    
    
  2. Generate value at run time

    int SQRT[65536];
    int main(){
        for(int i=0;i<65536;i++){
            SQRT[i]=sqrt(i);
        }
        //other code
        return 0;
    }
    
    

Some example of accessing them:

    if(SQRT[a*a+b*b]>something)
    ...

At start, I thought they should be the same, but I don't know if the program stores or access a hard-code array in different way. Also, I don't know if compiler will optimize the hard-code array to speed up the access time, is there performance difference between them when accessing the array?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire