1. 程式人生 > >ios中pthread_mutex和dispatch_semaphore效能對比

ios中pthread_mutex和dispatch_semaphore效能對比

因為自旋鎖有風險已經別踢出局不再使用,所以對比了一下pthread提供的pthread_mutex_t以及dispatch_semaphore。

測試時候特別注意debug模式和release模式,結果可能會完全不一樣。

測試方法

模擬實際使用的執行緒搶佔,分別在不同執行緒迴圈很多次呼叫鎖和解鎖,計算兩條執行緒都完成後的時間。

測試結果

debug模式下:

pthread_mutex_t : 79秒

dispatch_semaphore:85秒

release模式下:

pthread_mutex_t : 74秒

dispatch_semaphore:54秒

結論

dispatch_semaphore是pthread_mutex_t的1.37倍左右。

以release的為準,因為我們的程式最終是以release模式執行。

 

 

 

 

本文結束。