质量计算的PHP效率

质量计算的PHP效率,php,performance,Php,Performance,一个有趣的问题是,在Uni课程中,我们遇到了一个挑战,要模拟服务器接收请求,然后对其进行处理。我已经多次构建和重建这个项目,我认为已经尽我所能提高了它的效率,你能看到什么可以提高这个项目的效率吗 模拟针对$simulationLength运行,并且在每个$currentTime都有1/$arrival\u rate新请求到达的机会。大小为$maxQueueSize的队列保存这些请求,并在队列已满时拒绝它们。对于每个$currentTime,CPU都可以从$queue中删除一个项目,并在$exec

一个有趣的问题是,在Uni课程中,我们遇到了一个挑战,要模拟服务器接收请求,然后对其进行处理。我已经多次构建和重建这个项目,我认为已经尽我所能提高了它的效率,你能看到什么可以提高这个项目的效率吗

模拟针对
$simulationLength
运行,并且在每个
$currentTime
都有
1/$arrival\u 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);

?>


感谢您的时间,

微优化,但:

  • 在这样的重循环中,-$preIncrement比 $postIncrement++
  • 数组推送($queue,$currentTime);是一个函数调用 开销:使用$queue[]=$currentTime;反而
  • $currentTime+$executionTime被计算了两次,计算一次并存储结果,然后使用它

您能使用其他语言吗?PHP在数字运算方面并不以其速度著称。-如果你不能或不会使用不同的语言,也许可以研究一下。