Click to See Complete Forum and Search --> : [RESOLVED] Modular? PHP Maths
Nate1
08-08-2007, 04:27 PM
Im looking for the function that will say that if I have 55 records and I display 10 records per page that I will have 6 pages?
I think in C its called mod?? Can't remember what its called
ellisgl
08-08-2007, 04:41 PM
ceil(mysql_num_rows($sql) / 10); That will round up to the next whole number.
But I think you were talking about the modulus operand which is the percent sign - that only gives you the remainder of a the division.
Nate1
08-08-2007, 05:53 PM
Yeah Ceil was what I was Looking For
$pg = $I/10;
$pg = ceil($pg);
Thanks,
ellisgl
08-08-2007, 07:27 PM
Combine it on one line. Might be a tiny bit faster.
$pg = ceil($I/10);
Nate1
08-08-2007, 07:48 PM
Good point, should probably do that in alot of places, to much hack and slash.
Besides for the programmer do you know if it makes the operation easier for the server?
ellisgl
08-08-2007, 07:56 PM
well here's how I see it (I'm not an expert on how PHP works at the interpreter level):
$pg = $I/10;
$pg = ceil($pg);
Step 1: Get the out come $i/10, assign put into memory associated with $pg.
Step 2: Get $pg's memory contents, round it up then put it back into memory...
as with $pg = ceil($I/10);
Step 1: You calculate $I/10, round the answer up then put in memory associated with $pg.
Nate1
08-08-2007, 08:55 PM
Makes sense, I wonder? thanks
ellisgl
08-08-2007, 09:33 PM
you can test it actually..
<?php
function microtime_float()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$time_start = microtime_float();
for($x = 0; $x <= 100000; ++$x)
{
$pg = $I/10;
$pg = ceil($pg);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'test one: ',$time,' seconds<br />';
$time_start = microtime_float();
for($x = 0; $x <= 100000; ++$x)
{
$pg = ceil($I/10);
}
$time_end = microtime_float();
$time = $time_end - $time_start;
echo 'test two: ',$time,' seconds<br />';
?>
My test shows:
test one: 0.161412000656 seconds
test two: 0.148828029633 seconds
I cranked it up to 1000000 intervals and got:
test one: 1.65349602699 seconds
test two: 1.55738401413 seconds
I swap the excution order of the two and got this on 1000000 intervals:
test one: 1.49121499062 seconds
test two: 1.58176708221 seconds
So my method shaves ~.1 seconds off execution @ 1000000 intervals.. Which is ~10%
Nate1
08-08-2007, 09:48 PM
Cool thanks I might use that code in future,
ellisgl
08-08-2007, 09:52 PM
No problem. I've used it many times! Don't forget to mark the thread as resolved.
Nate1
08-08-2007, 10:12 PM
Sorry, Had a good look but probably using my male eyes, where do I do that?
ellisgl
08-08-2007, 10:53 PM
the timing script?