Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top