I add some Data in form of a table to my website. Actually everything works but i wanted to test the performance for some big data. It was very bad. Here my Results:
tablesize | time
-----------------
10x20 | 0.22s
100x20 | 3.29s
1000x20 | 28.75s
My question is which way is better to show the results and why:
1:
<?php
for($i=0; $i<count($array); $i++){
echo "<div id='...' class='...Ä style='...'>"
for($i=0; $i<count($array); $i++){
echo "<div style='...'>".array[$i][$j]."</div>";
}
echo "</div>";
}
?>
2:
<?php
for($i=0; $i<count($array); $i++){
?>
<div id='...' class='...Ä style='...'>
<?php
for($i=0; $i<count($array); $i++){
?>
<div style='...'> <?php echo array[$i][$j]; ?> </div>
<?php } ?>
</div>
<?php } ?>
Maybe someone has some additional tips how i can make the data visualization fast. I read some artical about MySQL structure and i believe my DB looks fine. So what i can do to optimize the reading of data.
As scaisEdge said, paginate your data.
Moreover, there are several changes that can make your code faster.
count($foo) outside of for loop
use pre-increment instead of post
use 'str' and concatenation instead of "str"
Here is a "benchmark" (without output) :
<?php
$array = array();
for($i = 0; $i < 1000; ++$i){
$array[] = range(0, 20, 1);
}
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
for($i=0; $i<count($array); $i++){
$a = "<div id='...' class='...Ä style='...'>";
for($j=0; $j<count($array[$i]); $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'Count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; $i++){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; $j++){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and post : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = "<div id='...' class='...Ä style='...'>";
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = "<div style='...'>".$array[$i][$j]."</div>";
}
$a = "</div>";
}
}
echo 'No count and pre : ' . (microtime(true) - $t) . "\n";
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
$c = count($array);
for($i=0; $i<$c; ++$i){
$a = '<div id="..." class="...Ä style="...">';
$c2 = count($array[$i]);
for($j=0; $j<$c2; ++$j){
$a = '<div style="...">'.$array[$i][$j].'</div>';
}
$a = '</div>';
}
}
echo 'No count and pre and \' : ' . (microtime(true) - $t) . "\n";
// #scaisEdge
$t = microtime(true);
for($k = 0; $k < 5000; ++$k){
foreach($array as $subarray){
$a = '<div id="..." class="...Ä style="...">';
foreach($subarray as $v){
$a = '<div style="...">'.$v.'</div>';
}
$a = '</div>';
}
}
echo 'Foreach : ' . (microtime(true) - $t) . "\n";
?>
This outputs :
Count and post : 46.050459861755
No count and post : 30.590306043625
No count and pre : 29.880299091339
No count and pre and ' : 29.930299043655
Foreach : 29.120290994644
As weird as it looks, ' seems to be slower than ".
PHP checks " strings variables inside and replace it whereas ' string are just... string. No operation performed.
Has someone an idea of this slowness ?
For large output you should use pagination ... output 1000 rows seems not useful
If you paginate your result is more equal to the firts that to the last .
But you can do somethings for the code .. you should count the result just one time and not for all cycle of the loop
so instead of for($i=0; i<count($array); $i++){
you should use
$numElem =count($array);
for($i=0; $i<$numElem; $i++){
.......
}
or use
foreach ( $array as $key => $value ){
......
}
these are much faster
Related
Write a PHP code to print following number pattern:
147
258
369
I am trying like this but its shows me shows below. how to convert column to row pattern.
<?php
$num = "";
for($i=1;$i<=9;$i++) {
$num .= $i;
if($i%3==0){
$num .="<br />";
}
}
echo $num;
?>
please help me
You need to use for loop inside for loop to achieve this. Below is the code
$num = "";
for( $i = 1; $i <= 3; $i++ ) {
for( $j = 0; $j <= 2; $j++) {
$k = $i + ( $j * 3 );
$num .= $k;
}
$num .= "<br />";
}
echo $num;
Here is another way to get this output.
for($i = 1; $i <= 3; $i++) {
$print = $i;
for($j = 1; $j <= 3; $j++) {
echo $print;
$print = $print + 3;
}
echo "<br />";
}
Can anyone help me understand why a variable takes its initial value after incrementing the variable? below is code:
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
echo $k+3;
echo $l+3;
}
In this we have two for loops running one inside other. Here we run three times the outside for loop, inside this we are running other for loop again. The problem we are facing is that when inner for loop end we have incremented $k and $l both by 3 but it always take value 0 and 3 respectively.
we have incremented $k and $l both by 3
Nope, you only print the result of your values plus 3, but you do not set them anywhere in the loop:
Instead of
echo $k+3;
echo $l+3;
write
$k = $k + 3;
$l = $l + 3;
You should try removing the "echo" and incrementing the variable in each loop. Then print them out after.
Try this:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
$j = $j++;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
?>
Gives you:
9
12
TRY this.
$k += 3;
$l += 3;
echo $k;
echo $l;
Try This
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k = $k+3;
$l = $l+3;
}
echo $k.'<br>';
echo $l;
First Increment the value and store it in the variable
$k = $k+3;
$l = $l+3;
Then You need to print using
echo $k.'<br>';
echo $l;
#Harinarayan First of all you need to read about echo() http://php.net/manual/en/function.echo.php
echo — Output one or more strings
echo does not manipulate expression as you did in your question like:
echo $k+3;
instead of using echo for the increment you should first increment the variable and then echo that variable like below:
<?php
$k= 0;
$l= 3;
for($i = 0; $i<3; $i++){
for($j = $k; $j<$l; $j++){
echo $j;
}
$k += 3;
$l += 3;
echo $k;
echo "<br>";
echo $l;
}
Please give me the solution for to print a triangle in stars pattern using php language. I used only 2 for loops to finish this.
*
***
*****
*******
Here is my code
<?php
for($a=1;$a<=7;$a++)
{
for($b=1;$b<=$a;$b++)
{
echo " *";
}
echo "<br/>";
}
echo " ";
?>
Please help me out.
<?php
for($i=0; $i < 5 ; $i++)
{
echo"<br>";
for($j=0; $j <=$i ; $j++){
echo "*" ;
}
}
?>
For more php Star patterns
This will work, still uses two for loops:
echo "<pre>";
$stars = "*";
for($a=1; $a<=4; $a++)
{
$spaces = "";
for($b = (5 - $a); $b>1; $b--)
{
$spaces .= " ";
}
echo $spaces . $stars . $spaces;
$stars .= "**";
echo "<br/>";
}
echo "</pre>";
I've used an accumulator for $stars, it starts from one star, an adds two stars at each iteration, and one accumulator for $spaces, that gets reset at each main for iteration.
The triangle is build top to bottom using one layer at a time.
One layer is made of spaces, stars and spaces again
for($x = 0; $x < $n; $x++) {
for($y = 0; $y < $n - $x; $y++) {
echo ' ';
}
for($z = 0; $z < $x * 2 +1; $z++) {
echo 'x';
}
echo "\n";
}
What I want to get with for loop. Something will be like this.
*
**
***
****
*****
****
***
**
*
This thing I want to create right now I have this code.
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
if($i == $end){
for($c=1; $c <= $end; $c++ ){
for($d=i; $d >= 2; $d--){
echo $star;
}
echo "<br>";
}
}
};
?>
And it's working fine with
*
**
***
****
*****
But then I need opposite loop first 4 then 3 then 2 then 1...
Where am I going wrong with this?
Just for fun. Try this:
$stars = 1;
for ($i = 0; $i < 9; $i++) {
for ($s = 1; $s <= $stars; $s++) {
echo "*";
}
echo "\n";
$stars += ($i<4)?1:-1;
}
And, for even more fun, one with just one for loop:
$stars = 0;
$starctr = 0;
for ($i = 0; $i < 25; $i++) {
echo "*";
if ($stars == $starctr) {
echo "\n";
$stars += ($i<14)?1:-1;
$starctr = 0;
} else {
$starctr++;
}
}
Can using nested for loop. Example:
$n = 5;
for($i = 1; $i <= $n; $i++){
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
}
for($i = $n-1; $i >= 1; $i--){
for($j = $i; $j >= 1; $j--){
echo '*';
}
echo '<br />';
}
Another technique using array_fill(), array_map(), array_reverse()
$n = 5; $arr = array();
for($i = 1; $i <= $n; $i++){
$arr[] = array_fill(0, $i, '*');
}
array_map(function($v){echo join('', $v) . '</br>';},$arr);
unset($arr[count($arr) - 1]); //remove last index value
array_map(function($v){echo join('', $v) . '</br>';},array_reverse($arr));
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
This can be achieved with only 2 for loops.
$offset = 1;
for ($i = 1; $i > 0; $i += $offset)
{
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
if ($i === 5)
$offset = -1;
}
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
};
for($c=$send; $c>=2; $c-- ){
for($d=$end; $d >= 2; $d--){
echo $star;
}
echo "<br>";
};
?>
Using a single for loop:
$end = 5;
$out = array();
for ($i=0;$i<$end;++$i)
{
$out[] = str_repeat('*', $i+1);
}
echo implode(PHP_EOL, $out).PHP_EOL;
array_pop($out);
$out = array_reverse($out);//remove last ****** <-- only one line with 5 stars
echo implode(PHP_EOL, $out).PHP_EOL;
Replace PHP_EOL with <br> if you want to, but this is the least loopy way to write this code I can think of
live demo
Here's a recursive attempt:
function ladder($n, $counter=1, &$acc=array())
{
if ($counter == ($n*2)) {
return implode($acc);
}
if ($counter <= $n) {
$acc[$counter] = str_repeat('*', $counter) . "\n";
$counter++;
}
if ($counter > $n) {
$diff = (int) $n-$counter;
$acc[$counter] = str_repeat('*', $n+$diff) . "\n";
$counter++;
}
return ladder($n, $counter, $acc);
}
print_r(ladder(5));
I'm try to print an array where every other value is reassigned, as examples (from this):
17.34502870451717,62.46137370987033
To this:
62.46137370987033,17.34502870451717
That part have I succeeded with, but now I have this structure:
[62.46137370987033,[17.34501402936927,]
[62.46123453616544,[17.34525377433593,]
[62.4610178881864,[17.34546663705899,]
This is where I get stuck and do not know how to write.
The structure I want looks like this:
[62.392628, 17.309413],
[62.393162, 17.309193],
[62.393403, 17.30922]
Here is my explode.php (GIST)
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i++) {
echo $wing . $minion[$i+1].",";
if($i%2==1) { echo "]<br />"; }
} echo $minion[0] . $wing;
?>
As I understand it, as long as there's always even pairs it should be as easy as;
<?php
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$eol = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
echo $eol.'['.$minion[$i+1].','.$minion[$i]."]";
$eol=',<br/>';
}
echo '<br/>';
>>> [62.46137370987033,17.34502870451717],
>>> [62.46123453616544,17.34501402936927]
Try This
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$wing = "[";
for ($i = 0;$i < count($minion) -1; $i+=2)
{
echo $kk = $wing . $minion[$i+1].",".$minion[$i]."],<br>";
}
Just a small modification to the given answers
$dwarf = "17.34502870451717,62.46137370987033,17.34501402936927,62.46123453616544";
$minion = explode(",",$dwarf);
$str = '';
for ($i = 0;$i < count($minion) -1; $i+=2) {
$str.='['.$minion[$i+1].','.$minion[$i].'],<br/>';
}
echo rtrim($str,','); // to trim ',' at the end