-1

So I have the little prime number calculator.

<?php
$number=$_GET['number'];
echo 'Input: '.$number.'<br><br>';

if((string)(int)$number == $number) {
        if($number>1 && $number<10000000){
            $status=0;
            $x=2;
            while($status==0){
                $y=$number/$x;
                if((string)(int)$y == $y){
                    $status=1;
                    echo 'Sorry. '.$number.' is not a prime number.';

                }
                if($x+1==$number){
                    $status=1;
                    echo 'Yes. '.$number.' is a prime number.';
                }
                $x++;


            }

    }
    else{
    echo 'Please specify a number between 1 and 10.000.000';
    }
}
else{
    echo 'Please specify a number. May not contain decimals, dots or commas.';
}

?>

How can I know how much server power it uses to calculate a high number. It could be nice if I could allow a higher number than 10 millions without people sucking all the resources.

Question: How do I know how long it took to perform the task and how do I know how many percent of the cpu and ram it used?

user3854743
  • 511
  • 1
  • 4
  • 9

1 Answers1

0

You will definitelly need some PHP plug-in for the CPU. But for the time it's like this:

$start = microtime(true);
//Start doing something
...

$stop = microtime(true);

echo "The stuff took ".($stop-$start)." seconds.";

And for the RAM, you can only get the peak (largest) and the current value.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • Okay thanks. Do you know if it would consume much power to calculate Eg. Up to 10 Billion? – user3854743 Aug 31 '14 at 22:20
  • Why don't you test it and tell us? Honestly, I don't have the slightest idea. It also depends on machine, doesn't it? – Tomáš Zato Aug 31 '14 at 22:22
  • sys_getloadavg() another usefull one –  Aug 31 '14 at 22:22
  • I'm not sure if this function reflects the activity of PHP script or whole server. – Tomáš Zato Aug 31 '14 at 22:23
  • @user3854743 Is there anything I could add to the answer? – Tomáš Zato Aug 31 '14 at 22:26
  • No. Its a little strange that it dosen't matter if I type 30 or 10000000000000. The time stays about the same. Oh and isnt it displayed in miliseconds? – user3854743 Aug 31 '14 at 22:37
  • Nope, `microtime(true)` returns time as float, that means seconds: `XX.XXXXX`. If the time is very short, the difference might be too small to notice. The speed of proccessing is also affected by other processes on the computer. – Tomáš Zato Aug 31 '14 at 22:39
  • Strange. Return would look like this. 6.9141387939453E-6 And the page loads in less than a second. Hows that possible? – user3854743 Aug 31 '14 at 23:10
  • Well, `E-6` means `*10^(-6)` - pretty damn short time. Less then a seccond is definitelly short time, so I'm not suprised. Check your code - I'm not a remote debugger. – Tomáš Zato Aug 31 '14 at 23:11