這篇文章主要介紹了關於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 秒
可見,效果差別懸殊。
以上就是本文的全部內容,希望對大家的學習有所協助,更多相關內容請關注topic.alibabacloud.com!