Jump to content

help with script


steviez

Recommended Posts

Hi,

 

My host has just susspended my account for overloading the cpu with my script, the only active PHP code i have at the moment is a random array that returns a image randomly on the front page. Can you please have a look and tell me if it would be causing the overload?

 

Code:

 

<?php
// Throw out a random image
$pic = array("front_pic_1.jpg"=>"Front_Pic_1","front_pic_2.jpg"=>"Front_Pic_2","front_pic_3.jpg"=>"Front_Pic_3","front_pic_4.jpg"=>"Front_Pic_4","front_pic_5.jpg"=>"Front_Pic_5");
print_r(array_rand($pic,1));
?>

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/111712-help-with-script/
Share on other sites

pretty sure that unless that "script" is being called many thousands of times per second, it's not causing a problem.

 

On my Core 2 Duo Laptop running Fedora 8, I ran the your code above one million times in 14.01 seconds, during that time, it consumed 48% CPU...or about 96% of one core.

 

At that rate, your code would have to be called 71428 times per second to consume 100% of CPU resources.

 

I think that either the problem lies somewhere else, or you need a new host who doesn't whine when you use more than 2% cpu.

 

<?php

$iterations = 1000000;
$start = microtime(true);

for ($i = 0; $i < $iterations; $i++) {
// Throw out a random image
$pic = array(
	"front_pic_1.jpg"=>"Front_Pic_1",
	"front_pic_2.jpg"=>"Front_Pic_2",
	"front_pic_3.jpg"=>"Front_Pic_3",
	"front_pic_4.jpg"=>"Front_Pic_4",
	"front_pic_5.jpg"=>"Front_Pic_5"
);

print_r(array_rand($pic,1));
}

echo "\n\nTotal time for " . $iterations . " iterations: " . (microtime(true) - $start) . " seconds";

?>

Link to comment
https://forums.phpfreaks.com/topic/111712-help-with-script/#findComment-573768
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.