0

I have got a strange problem.This is my code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<?php
$fp=fopen("test.txt","w",true);
if($fp)
{
$start=time();
for($i=0;$i<=17800;$i++)
{
    fwrite($fp,"bandwith");
}
fclose($fp);
$stop=time();
$diff=round($stop-$start,2);
$fsize=round(filesize("test.txt")/1024,2);
$bandwidth=$fsize/$diff;
echo "<strong>The speed of your broadband is</strong> ".$bandwidth;
}
else
{
    echo ("problem with permission");
}
?>

</body>
</html>

I am actually trying to calculate the bandwidth.The error I get is "Division by zero" on the $bandwidth calculation part.The strange part is that when I keep on refreshing the window I suddenly get the answer then again the problem comes during the next refresh.

Extra info:

I found out that the error occurs when the $start and $stop are same.Now my question is:Is localhost so damn fast to finish the text file opening and writing part within the same time resulting in division my zero error?I also checked the text file and it contains the data and it is deleted and written every other time.I just want a clear explanation of the reason behind it.

user1633170
  • 11
  • 2
  • 13

3 Answers3

2

If you need a more accurate time calculation, and your system supports it, you can use microtime:

$time_including_microseconds = microtime();

See the example in the manual about exploding it.

jeroen
  • 91,079
  • 21
  • 114
  • 132
  • I know that.I want to know is it possible for the localhost to finish the above task of creating and writing a file in that speed of 0 seconds difference? – user1633170 Aug 29 '12 at 17:29
  • @user1633170 Sure, 0.49999999999... seconds before and after a whole second is a long time to write a file on the local system. – jeroen Aug 29 '12 at 17:30
  • @user1633170 jeroen has a lot of rep, which indicates he's pretty reliable. Try his method. – Matt Aug 29 '12 at 17:31
  • @user1633170 By the way, you should always account for a division by zero, even when you use micro-seconds. – jeroen Aug 29 '12 at 17:36
0

You can use this function to measure file access time:

http://php.net/manual/en/function.filemtime.php

Doug Molineux
  • 12,283
  • 25
  • 92
  • 144
  • I think you didn't understand my question.I don't want file modification time.I am just calculating the time taken to finish the task. – user1633170 Aug 29 '12 at 17:26
0

As the comment says, it's very likely your file operation (writing) is being completed in less than a second, resulting in the division by zero.

To accomplish your goal, considering using PHP's microtime function. This includes milliseconds with the timestamp.

See also: Accurate way to measure execution times of php scripts

Community
  • 1
  • 1
Eric G
  • 4,018
  • 4
  • 20
  • 23