PHP Math Calculations Are Very Slow, Any Solution?

I will explain by showing two code snippets.
SNIPPET 1 - (Page Loading Time - 11.5 seconds )

<?php
	$u = 1 ;				
	while($u < 7):
	$sub = "-$u days" ;
  ?>
<tr>
	<td> <?php echo date("Y-m-d",strtotime($sub, time())); ?> </td>
	<td> <?php echo $chf = convert_ch($from_sym,$to_sym,$val,$sub) ; ?> </td>
	<td> <?php echo ($current_ans - $chf)?></td>
	<td> <?php echo round((1 - $current_ans / $chf) * 100)?> </td>
</tr>
						
<?php $u++ ; endwhile;  ?>

SNIPPET 2 - (Page Loading Time - 3 seconds )

<?php	
$u = 1 ;				
	while($u < 7):
	$sub = "-$u days" ;
  ?>
<tr>
	<td> <?php echo date("Y-m-d",strtotime($sub, time())); ?> </td>
	<td> <?php echo $chf = convert_ch($from_sym,$to_sym,$val,$sub) ; ?> </td>
	<td> <?php // echo ($current_ans - $chf)?></td>
	<td> <?php // echo round((1 - $current_ans / $chf) * 100)?> </td>
</tr>
						
<?php $u++ ; endwhile;  ?>

There is a difference of 8 seconds just by commenting out the calculation statements but I need those calculations. Can you please tell me a solution for it ??

Hi @vinit,
I’m not sure what’s the problem by looking at that code… what I could figure is that potentially the problem is within this function: convert_ch
Any possibility we could take a look at it?

thanks, after reading your reply I tried setting a static value to variable $chf and yes my loading time decreased to 3.4 seconds.

Still 3.4 seconds seems too much for that operation, seems maybe those are due to a slow connection or some other code?

3.4 seconds is the complete page loading time which also includes other code and other resources. So I think 3.4 seconds is fine here . but I am still unable to figure out the issue with convert_ch function.

can you please help me ?? if I paste the function here

Will have a go and if i can’t help I’m sure someone else will

1 Like

Thanks for your precious time @Andres_Vaquero.
This is my convert_ch function

function convert_ch($from,$to,$val,$time){
	global $is_from_coin ;
	global $is_to_coin ;
	if(!$is_from_coin&&!$is_to_coin){
		$fromp = get_price($to,$time) ;
 		$top = get_price($from,$time) ;	
	}elseif($is_from_coin&&!$is_to_coin){
		$fromp = get_price($from,$time) ;
 		$top = get_price("usd",$time) ;	
	}else{
		$top = get_price($to,$time)  ;
 		$fromp = get_price($from,$time)  ;	
	}
	$r = 0  ;
	$dec = 3 ;
	
	if($top == null || $fromp == null)
		return false ;
		
	
	$r = number_format(($fromp / $top) * $val,7) ;
	
	$r = str_replace(",","", $r);
	if($is_from_coin&&!$is_to_coin){
		$r = convert_ch("usd",$to,$r,$time); 
	}
	return ($r * 1 ) ;
}

Maybe it is due to the fact that you have a recursive call… You’re calling convert_ch inside of itself… I would definitely review that, as you’re probably making thousands and thousands of calls to it and that is most certainly why the script is slow

2 Likes

Yes, that function definitely looks like it could use some improvement. Where did you get that script, is there an alternative?

Try this because if may reduce the amount of times PHP is called from HTML

<?php 
  declare(strict_types=1);
  error_reporting(-1);
  ini_set('display_errors', 'true');

# DECLRE VARIABLES
  if(1):
    $tmp1 = [];
    $tmp2 = [];
    $tmp3 = [];
    $tmp4 = [];
    $from_sym = $to_sym = $val = $sub = 42;
    $current_ans = $chf =88;
  endif;  

  $u = 0;        
  while( $u <= 7):
    $sub    = "$u days" ;
    $tmp1[] = date("Y-m-d", strtotime($sub, time() ) );
    $tmp2[] = $chf = convert_ch($from_sym, $to_sym, $val,$sub);
    $tmp3[] = $current_ans - $chf;
    $tmp4[] = round( (1 - $current_ans / $chf) * 100);
    $u++;
 endwhile;  

 $rows = '';
 foreach( $tmp1 as $id => $dummy):
  $rows = '<tr>' 
        .   '<td>' .$tmp1[$id] .'</td>' 
        .   '<td>' .$tmp1[$id] .'</td>'
        .   '<td>' .$tmp1[$id] .'</td>'
        .   '<td>' .$tmp1[$id] .'</td>'
        .' </tr>'
        ;
  endforeach;

  echo '<br>$rows ==> ';
  echo htmlentities($rows);            

# DEBUG
  if(1):
    echo '<br>';
    fred( $tmp1, '$tmp1');
    fred( $tmp2, '$tmp2');
    fred( $tmp3, '$tmp3');
    fred( $tmp4, '$tmp4');
  endif;  

//==============================================
function fred($val, $title=NULL)
{
  echo '<pre>';
    echo $title .' ==> ';
    print_r( $val );
  echo '</pre>';
}

//==============================================
function convert_ch($from,$to,$val,$time){
  global $is_from_coin ;
  global $is_to_coin ;

  if(!$is_from_coin && !$is_to_coin){
    $fromp = get_price($to,$time) ;
    $top   = get_price($from,$time) ; 

  }elseif($is_from_coin && !$is_to_coin){
    $fromp = get_price($from,$time) ;
    $top = get_price("usd",$time) ; 

  }else{
    $top = get_price($to,$time)  ;
    $fromp = get_price($from,$time)  ;  
  }
  $r = 0  ;
  $dec = 3 ;
  
  if($top == null || $fromp == null):
    return false ;
  endif;  

  $r = number_format(($fromp / $top) * $val,7) ;
  $r = str_replace(",","", $r);
  if($is_from_coin&&!$is_to_coin):
    $r = convert_ch("usd",$to,$r,$time); 
  endif;

  return ($r * 1 );
}

//==============================================
function get_price($to,$time)
{
  return 888;
}

Edit:
Added DEBUG $tmp1, $tmp2, $tmp4, $tmp4 varaibles.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.