2013-10-20 47 views
3

雖然我瞭解apc_store和apc_add之間的區別,但我想知道使用其中一種還是具有任何性能優勢?使用apc_store vs apc_add的任何性能優勢(反之亦然)?

有人會認爲apc_store可以快一點,因爲它不需要做一個檢查,如果存在之前做插入。

我的思想正確嗎?

或者在我們知道確認條目不存在的情況下使用apc_add證明速度更快?

回答

0

Short:apc_store()應該比apc_add()略慢。

更長:兩者之間的唯一區別是exclusive標誌傳遞給apc_store_helper(),反過來導致apc_cache_insert()中的行爲差異。

這裏是發生了什麼有:

if (((*slot)->key.h == key.h) && (!memcmp((*slot)->key.str, key.str, key.len))) { 
    if(exclusive) { 
     if (!(*slot)->value->ttl || (time_t) ((*slot)->ctime + (*slot)->value->ttl) >= t) { 
      goto nothing; 
     } 
    } 
    // THIS IS THE MAIN DIFFERENCE 
    apc_cache_remove_slot(cache, slot TSRMLS_CC);** 
    break; 
} else 
    if((cache->ttl && (time_t)(*slot)->atime < (t - (time_t)cache->ttl)) || 
     ((*slot)->value->ttl && (time_t) ((*slot)->ctime + (*slot)->value->ttl) < t)) { 
     apc_cache_remove_slot(cache, slot TSRMLS_CC); 
     continue; 
    } 

    slot = &(*slot)->next;  
} 

if ((*slot = make_slot(cache, &key, value, *slot, t TSRMLS_CC)) != NULL) { 
    value->mem_size = ctxt->pool->size; 

    cache->header->mem_size += ctxt->pool->size; 
    cache->header->nentries++; 
    cache->header->ninserts++; 
} else { 
    goto nothing; 
} 

的主要區別在於apc_add()節省了一個插槽去除如果該值已經存在。真實世界的基準顯然會對確認分析有很大的意義。