2012-04-18 56 views
2

一個有趣的問題是,對於一個Uni課程,我們一直在接受請求並處理它們的服務器仿真運行。我已經多次構建和重建,我認爲儘可能提高效率,有沒有什麼可以提高此程序的效率?PHP質量計算效率

模擬運行$simulationLength和每個$currentTime有一個新的請求到達1/$arrival_rate的機會。大小爲$maxQueueSize的隊列保存這些請求,並在隊列已滿時拒絕它們。對於每個$currentTime,CPU可能會從$queue中刪除一個項目,並在$executionTime秒內處理該項目,此時CPU(服務器)將從繁忙隊列中刪除,並可以自由處理其他請求。

我的代碼:

<?php 

...SNIP error checking... 

$queue_size = isset($argv[1]) ? $argv[1] : 10000; 
$arrival_rate = isset($argv[2]) ? $argv[2] : 3; 
$number_of_servers = isset($argv[3]) ? $argv[3] : 2; 
$execution_time = isset($argv[4]) ? $argv[4] : 25; 

...SNIP error checking... 

$simulationLength = 1000000; 

$arrivalRate = $arrival_rate; 
$executionTime = $execution_time; 

$busyServers = array(); 
$freeServers = $number_of_servers; 

$currentTime = 0; 
$currentQueueSize = 0; 
$maxQueueSize = $queue_size; //Max 
$queue = array(); 

//Stats 
$totalRequests = 0; 
$rejectedRequests = 0; 
$queueSize = array_fill(0, 100, $simulationLength); 
$totalWaitingTime = 0; 

//while the simulation is running 
while($currentTime++ < $simulationLength) { 

    //a request has arrived 
    if(mt_rand(1, $arrival_rate)==1) { 
    $totalRequests++; 

    //Adding to the queue 
    if($currentQueueSize < $maxQueueSize) { 
     $currentQueueSize++; 
     array_push($queue, $currentTime); //add to the end of the queue 
    } else { 
     $rejectedRequests++; 
    } 

    } //end request arrived 

    //Only one server can fetch requests at a time 
    if(!empty($busyServers)&&reset($busyServers) < $currentTime) { 
    unset($busyServers[key($busyServers)]); //remove first element - efficient 
    $freeServers++; //increase free servers 
    } 

    //Only one server can fetch requests at a time 
    if($currentQueueSize>0&&$freeServers>1) { 

    $currentQueueSize--; 
    reset($queue); //reset pointer 
    $queueTime = $queue[key($queue)]; //read of the front 
    unset($busyServers[key($busyServers)]); //delete off print 

    $freeServers--; //use up a server 

    $busyServers[] = $currentTime + $executionTime; //mark when free 

    $totalWaitingTime += ($currentTime - $queueTime) + $executionTime; 
    } 

    $queueSize[$currentTime] = $currentQueueSize; 
} 

printf("Requests served %d\n", $totalRequests); 
printf("Rejected requests %d\n", $rejectedRequests); 
printf("Total waiting time %d\n", $totalWaitingTime); 

printf("Percentage of rejected requests %0.1f\n", $rejectedRequests/$totalRequests); 
printf("The average queue size %d\n", array_sum($queueSize)/sizeof($queueSize)); 
printf("Average response time %d\n", $totalWaitingTime/$totalRequests); 

?> 

感謝您的時間,

+1

你可以使用任何其他語言? PHP在數值運算方面的速度並不爲人所知。 – Blender 2012-04-18 21:10:56

+0

http://www.phpbench.com - 希望頁面幫助 – Hajo 2012-04-18 21:15:04

+0

如果您不能或不會使用不同的語言,請查看[HipHop-PHP](https://github.com/facebook/hiphop -php /維基/)。 – 2012-04-18 21:42:31

回答

1

微的優化,但是:

  • 當沉重的循環這個樣子, - $前增量比 快$ postIncrement ++
  • array_push($ queue,$ currentTime);是一個函數調用 開銷:使用$ queue [] = $ currentTime;而不是被計算兩次
  • $ currentTime的+ $ executionTime,一旦計算並存儲結果,然後使用該