At first glance, this might seem counter-intuitive to some, since in the second instance, entity_get_controller('node')->resetCache() is actually being called for every single node load. But that function is so efficient it cost practically nothing, so the real cost is in the additional memory consumption and index look-ups that are involved in the first set of code above, when the number of nodes is large.
Update: Just to be clear, my simplified code snippets above assume you're doing more work inside the loops, and that you really do need each node individually read from the database at that moment. If you just want to load a bunch of nodes, or if you can afford momentary caching of the whole group of nodes, you should of course be using node_load_multiple(). And yes, the same principle as above applies: you gain nothing by calling entity_get_controller('node')->resetCache(); yourself. Just pass the $reset argument as TRUE to skip the cache. (Thanks to bangpound for helping me see the potential confusion I might have created.)
Bad:
entity_get_controller('node')->resetCache();
foreach ($result AS $nid) {
node_load($nid);
//do stuff
}
Good:
foreach ($result AS $nid) {
node_load($nid, NULL, TRUE);
//do stuff
}
At first glance, this might seem counter-intuitive to some, since in the second instance, entity_get_controller('node')->resetCache() is actually being called for every single node load. But that function is so efficient it cost practically nothing, so the real cost is in the additional memory consumption and index look-ups that are involved in the first set of code above, when the number of nodes is large.
Update: Just to be clear, my simplified code snippets above assume you're doing more work inside the loops, and that you really do need each node individually read from the database at that moment. If you just want to load a bunch of nodes, or if you can afford momentary caching of the whole group of nodes, you should of course be using node_load_multiple(). And yes, the same principle as above applies: you gain nothing by calling entity_get_controller('node')->resetCache(); yourself. Just pass the $reset argument as TRUE to skip the cache. (Thanks to bangpound for helping me see the potential confusion I might have created.)