note 62204 rejected from function.sqrt by tularis
| From: | tularis@php.net | Date: | Wed, 22 Feb 2006 12:44:09 +0000 |
| Subject: | note 62204 rejected from function.sqrt by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-104216@lists.php.net to get a copy of this message | ||
Note Submitter: kore
----
The codesnippet below for "real aquare roots" is a really bad implementation.
<?php
$limit = 10000;
for ($i = 1; $i <= $limit; ++$i) {
$sq_root = sqrt($i);
if ((int) $sq_root == $sq_root) echo "$sq_root is square root of $i\n";
}
?>
Is about 70 times faster, because there is no typecasting to string, and no use of ereg (one of the
slowest and most useless functions, and depreceated by the way). If you want to test for a substr
use strpos, or preg_match, if you need regular expressions.
The other way round, even more sensefull, is about 150 times faster:
<?php
$limit = 10000;
$i = 1;
while (($pow = $i * $i) <= $limit) echo $i++, " is square root of $pow\n";
?>