mardi 24 mars 2015

Speed of accessing a pointer


I'm rewriting all of the data structures I learned in school as an exercise to reinforce my understanding of the fundamentals of computer science, but I'm attempting to optimize my naïve implementations that I created when I was younger.


This lead me to a question:


I have these few lines from my linked list implementation where temp is used in both variations...



Node<T> *temp = head;

if(temp == nullptr) {
...
}

... // temp used here


My linked list also has an attribute long listSize, so the above line is analog to



if(!listSize) {
...
}

... // temp used here, too.


This is obvious. What is less obvious (to me) is which of these would be faster for the computer.


From what (little) I understand about pointers, any operation with them is fairly expensive, which leads me to believe that the former might be less optimal than the latter; however, I'm not entirely sure.


Can anyone help clear up this nuance for me?


Thanks, erip




Aucun commentaire:

Enregistrer un commentaire