這篇文章主要介紹了關於Centos下循環測試php對Redis和共享內存(shm)讀寫的效率,有著一定的參考價值,現在分享給大家,有需要的朋友可以參考一下
redis和memcache還有共享記憶體都是讀取記憶體的數據,為了測試一下到底效率誰更勝一籌,我在我的Centos虛擬機下做了一次公平的測試。
測試參數
環境:Centos (設定忽略)、語言:PHP、WebServer:Nginx、測試次數:10000、字元類型及長度:字串(1024 )
準備測試
<?php //测试数据 $arr = array( 1 => array ('id' => '4146','attacksdfdsfdsddddddddddd' => '5.45','atk_class' => '神圣级','wufang' => '4.9','def_class' => '超神级','cefang' => '4.9','mdf_class' => '超神级','hp' => '6.43','hp_class' => '神王级','dodge' => '55','dodge_class' => 'SSSS','crit' => '50','crit_class' => 'SSS','penetrate' => '55','penetrate_class' => 'SSSS','mingzhong' => '51','hit_class' => 'SSSS','anti_dizziness' => '0','anti_dizzinesssdfdsfdsfdsfdsfds_class' => 'F','anti_critical' => '20','anti_critical_class' => 'F','tenacity' => '0','tenacity_class' => 'F', ), 2 => array ('id' => '414ddffff7','attack' => '5.45','atk_class' => '神圣级','wufang' => '4.95','def_class' => '超神级','cefang' => '4.95','mdf_class' => '超神级','hp' => '6.43','hp_class' => '神王级','dodge' => '55','dodge_class' => 'SSSS','crit' => '50','crit_class' => 'SSS','penetrate' => '55','penetrate_class' => 'SSSS','mingzhong' => '51','hit_class' => 'SSSS','anti_dizziness' => '0','anti_dizziness_clsdfdsfdsfdsfdsfdsass' => 'F','anti_critical' => '20','anti_critical_class' => 'F','tenacity' => '0','tenacity_class' => 'F', )); $str = json_encode($arr); echo strlen($str); //输出1024
對redis進行10000次寫入測試
<?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->auth('310c8cabcdefghf2d8abcdefd44496ac80'); $start = microtime(true) ; for($i = 0; $i<10000; $i++) { $redis->set('key', $str); } echo microtime(true) - $start; //执行5次,大概平均结果为 1.7016470432281 秒
對共享記憶體(shm)進行10000次寫入測試
<?php $key = 0x4337b123; $size = 1024; $shmid = @shmop_open($key, 'c', 0644, $size); $start = microtime(true); for($i = 0; $i<10000; $i++) { shmop_write($shmid, $str, 0); } @shmop_close($shmid); echo microtime(true) - $start; //执行5次,大概平均结果为 0.0025370121002197 秒
對redis進行10000次讀取測試
<?php $redis = new Redis(); $redis->connect('127.0.0.1',6379); $redis->auth('310c8cabcdefghf2d8abcdefd44496ac80'); $start = microtime(true) ; for($i = 0; $i<10000; $i++) { $redis->get('key'); } echo microtime(true) - $start; //执行5次,大概平均结果为 2.1236310005188 秒
對共享記憶體(shm)進行10000次讀取測試
<?php $key = 0x4337b123; $size = 1024; $shmid = @shmop_open($key, 'c', 0644, $size); $start = microtime(true); for($i = 0; $i<10000; $i++) { shmop_read($shmid, 0,1024); } @shmop_close($shmid); echo microtime(true) - $start; //执行5次,大概平均结果为 0.0021078586578369 秒
結果清單
#Redis 讀:2.1秒 寫:1.7秒
Shm 讀:0.0021秒 寫:0.0025 秒
1025 秒##11625 秒時可見,效果不符。 以上就是本文的全部內容,希望對大家的學習有所幫助,更多相關內容請關注PHP中文網! 相關推薦:PHP程式碼自動偵測(git/ svn整合PHP_CodeSniffer)
以上是Centos下循環測試php對Redis和共享記憶體(shm)讀寫的效率的詳細內容。更多資訊請關注PHP中文網其他相關文章!