Performance

Use cache_set and cache_get


/**
 * Demo cache.
 * 
 * Get all articles from cache table
 * or load them using node_load when cache isn't set.
 * 
 * Time example is for 50 articles.
 */
function _demo_cache() {
 
  if(!cache_get('demo_cached_articles')) {
    timer_start('Demo cache -- No cache');
  
    $nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->execute()->fetchCol();
   
    $nodes = node_load_multiple($nids);
 
    // Average is 22 ms to load 50 articles.
    dpm(timer_stop('Demo cache -- No cache'));
    

The fastest way to get nids in a default no cache situation

In this test case scenario a function was made to show the differences when querying for node ids. Mostly this kind of query is the first step to render nodes in a custom made view. Each query returns the same array of node ids. Execution times are tested by using the Drupal 7 bootstrap timer functions.

Performance - using the bootstrap timer functions

Use timer_start(), timer_read() and timer_stop() to measure code performance

These practical and easy to use performance measuring functions can be found in the bootstrap.inc file, provided by Drupal core. The same timer can be started and stopped multiple times, the measured intervals will be accumulated.

Create a lightweight commerce cart block

We build a custom block and use hook_theme to render the cart data. Usually a view is made to create a custom shopping cart block. However, a custom block to render number of items and the total price of an order is more exciting to make and more performant than a view.

Subscribe to RSS - Performance