php - Why don't loops scale evenly? -


i not believe duplicate, i've looked it, had no clue call exactly.

i want know why loop ten times larger loop doesn't take ten times longer run.

i doing testing try , figure out how make website faster , more reactive, using microtime() before , after functions. on website, i'm not sure how pull lists of table rows attributes out without going through entire table, , wanted know if slowing me down.

so using following loop:

echo microtime(), "<br>"; echo microtime(), "<br>"; session_start(); $connection = mysqli_connect("localhost", "root", "", "") or die(mysqli_connection_error());;  echo microtime(), "<br>"; echo microtime(), "<br>"; $x=1000; $messagequery = mysqli_query($connection, "select * users id='$x'"); while(!$messagequery or mysqli_num_rows($messagequery) == 0) {     echo('a');     $x--;     $messagequery = mysqli_query($connection, "select * users id='$x'");     } echo "<br>"; echo microtime(), "<br>"; echo microtime(), "<br>"; 

i got following output , similar outputs:

0.14463300 1376367329 0.14464400 1376367329 0.15548900 1376367330 0.15550000 1376367330 < these 2 [a's omitted, readability] 0.33229800 1376367330 < these 2 0.33230700 1376367330 

~18-20 microseconds, not bad, nobody notice that. wondered happen website grew. happen if had 10 times many (10,000) table rows search through?

0.11086600 1376367692 0.11087600 1376367692 0.11582100 1376367693 0.11583600 1376367693 [lots of a's] 0.96294500 1376367694 0.96295500 1376367694 

~83-88 microseconds. why isn't 180-200 microseconds? take time start , stop loop or something?

update: see if mysql adding variables, tested without mysql:

echo microtime(), "<br>"; echo microtime(), "<br>"; session_start(); $connection = mysqli_connect("localhost", "root", "w2072a", "triiline1") or die(mysqli_connection_error());;  echo microtime(), "<br>"; echo microtime(), "<br>"; $x=1000000; while($x > 10) {     echo('a');     $x--;     } echo "<br>"; echo microtime(), "<br>"; echo microtime(), "<br>"; 

now appears @ 1 million, takes ~100 milliseconds(right?) , @ ten million takes ~480 milliseconds. so, question still stands. why larger loops move more quickly? it's not important, i'm not planning entire website design based off of this, interested.

normally, loops scale linearly.

possible bug: if haven't done so, consider might happen if there no record id 900.

i recommend using mysql filtration work via clauses rather sorting thru information way. it's not scalable.

frankly, line

while(!$messagequery or mysqli_num_rows($messagequery) == 0) {

doesn't make sense me. $messagequery false if failure occurs, , want loop run long mysqli_num_rows($messagequery) not equal zero, think. however, that's not above code does.

if mysqli_num_rows($messagequery) equal zero, loop continue.

if mysqli_num_rows($messagequery) not equal zero, loop stop.

see operator precedence: http://php.net/manual/en/language.operators.precedence.php

does answer question?


Comments

Popular posts from this blog

ios - UICollectionView Self Sizing Cells with Auto Layout -

node.js - ldapjs - write after end error -

DOM Manipulation in Wordpress (and elsewhere) using php -