note 62204 added to function.sqrt

From: Date: Wed, 22 Feb 2006 09:57:02 +0000
Subject: note 62204 added to function.sqrt
Groups: php.notes 
Request: Send a blank email to php-notes+get-104210@lists.php.net to get a copy of this message
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"; ?> ---- Server IP: 217.13.201.10 Probable Submitter: 217.194.224.41 ---- Manual Page -- http://www.php.net/manual/en/function.sqrt.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62204 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62204&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62204&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104210) next »