0

the question may be stupid but its really important for me to understand how i can do it in the right way.

Let's say that we have the following script

<?php

//set count_bytes
$count_bytes = true;
//set count_bytes


$bytes = 0;
$url = "http://127.0.0.1:" . 8000;
$file_handler = @fopen( $url, "rb" ) or die( "Not Working" );

foreach ( $http_response_header as $h )
{
    header( $h );
}

while ( ! feof( $file_handler ) )
{
    $response = stream_get_line( $file_handler, 4096 );

    if($count_bytes === true)
    {
        $bytes += strlen( $response );
    }
    echo $response;
}

fclose($file_handler);

?>

As you can see i am using the variable $count_bytes which is bool. In the while loop you can see that the if statement is being checked everytime. Is there any way to optimize this so the check will only be done for one time?

I guess the one solution is to make the while loop 2 times and call the one we want depending on the bool value of $count_bytes but that is too much code and of course if we have huge code this is bad.

Thanks hope you understand me

user3393046
  • 163
  • 1
  • 7
  • 15
  • 6
    This question appears to be off-topic because it is more suited for http://codereview.stackexchange.com/ – Gerald Schneider May 09 '14 at 13:19
  • Have you measured to find out how much time you'll save overall by avoiding a simple boolean comparison in a loop that's pulling data over a network? (Hint: not very much.) – Matt Gibson May 09 '14 at 13:21
  • Well in this example it is simple boolean comparison. it is unoticed even for 1000000000 loops. But this is just an example. What i want is general since i may have other statements more time consuming etc. – user3393046 May 09 '14 at 13:22
  • Unfortunately, this is not a good example: the answer is likely to change as things become more complicated, so there isn't a general case, really. Try asking again when you have an example where the optimisation might actually make a difference. – Matt Gibson May 09 '14 at 13:25
  • this is already pretty optimized. In other cases, where there is a more complex condition, it may be worth it to evaluate the condition before the loop and use a boolean in the loop. Sometimes it just is necessary to have checks versus config-like options in loops if you want to avoid massive parallel code – cypherabe May 09 '14 at 13:26
  • Or do it in an object-oriented fashion: have the non-counting case done in a base class, and inherit from that to add a version that does the byte-counting, too, factoring out the common code into the base class so you don't repeat it. The trouble is, it's hard to give the right advice for a trivial example where nothing you do will actually make any significant improvement. – Matt Gibson May 09 '14 at 13:29
  • @MattGibson , well there is a strlen function as well inside if statement if it is true so it can make a difference i think in huge loops. – user3393046 May 09 '14 at 13:31
  • But that's not what you asked: when you want to count bytes, you'll aways be doing the strlen(). When you don't want to count bytes, you'll never be doing the strlen(). You're only asking about moving the boolean comparison out of the loop, aren't you? The amount of times you call strlen() will be the same whether the boolean check is inside the loop or outside it. – Matt Gibson May 09 '14 at 13:32
  • wow yes i am dump sorry. Yes its just the if statement. the strlen doesnt matter. – user3393046 May 09 '14 at 13:33

1 Answers1

1

Leave it as it is.

What exactly is code branching

Code branching is used by all modern processors and it makes situations like yours not matter if the if statement is there or not

Code branching leaves the hardware to make guesses at if the if statement will be true or false. If it guesses right, there is a speed boost, If it guesses wrong, there is a slow down. In your case it will guess right 100% of the time

Community
  • 1
  • 1
exussum
  • 18,275
  • 8
  • 32
  • 65