Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3938400
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T00:10:50+00:00 2026-05-20T00:10:50+00:00

Can anyone tell me why when I ran a script with the below contents

  • 0

Can anyone tell me why when I ran a script with the below contents and then stop it after 5 seconds that I need to divide the elapsed time by 2 to get the correct script execution time?

ignore_user_abort(true); set_time_limit(0); 

$begin_time = microtime(true);

$elapsed_time = 0;

while(!connection_aborted()) {
    echo ' ';
    flush();
    usleep(1000000);
}

$elapsed_time = microtime(true) - $begin_time;

$timer_seconds = $elapsed_time; //10 seconds

$timer_seconds = $elapsed_time / 2; //5 seconds


/*I am writing to a DB - but you can use this to test */
$fp = fopen('times.txt', 'w');
fwrite($fp, 'Time Elapsed: '.$timer_seconds);
fclose($fp);

Feel free to try the code as it has baffled me on why the $elapsed_time needs to be divided by two. Maybe I have misunderstood something?

Thanks all for any help

Update

I have updated the code so that anyone can try this out and it will write to a text file to view the output.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T00:10:50+00:00Added an answer on May 20, 2026 at 12:10 am

    Experiment:

    Significant changes from original code:

    1) Using implicit_flush and all buffers are flushed before doing anything.
    2) Instead of outputting just a space, the code outputs the iteration number and 1023 bytes of other data to tell the browser that we have good amount of output to display. A normal known trick.
    3) Along with saving the time in the output text file, it also saves the total iterations that the code ran.

    The code used:

    <?php
    // Tricks to allow instant output
    @ini_set('implicit_flush', 1);
    for ($i = 0; $i < ob_get_level(); $i++)
        ob_end_flush();
    ob_implicit_flush(1);
    
    //Your Code starts here
    ignore_user_abort(true);
    set_time_limit(0); 
    
    $begin_time = microtime(true);
    $elapsed_time = 0;
    
    while(!connection_aborted())
    {
        //this I changed, so that a looooong string is outputted
        echo $i++.str_repeat(' ',1020).'<br/>';
        flush();
        usleep(1000000);
    }
    
    $elapsed_time = microtime(true) - $begin_time;
    $timer_seconds = $elapsed_time; //10 seconds
    
    //Writes to file the number of ITERATIONS too along with time
    $fp = fopen('4765107.txt', 'w');
    fwrite($fp, 'Time Elapsed: '.$timer_seconds);
    fwrite($fp, "\nIterations: ".$i);
    fclose($fp);
    ?>
    

    Live Demo:

    • Now, here is the code to run:
      http://work.sktnetwork.com/so/4765107.php

    • And here is the .TXT file it generates:
      http://work.sktnetwork.com/so/4765107.txt


    What I got:

    1) When code is run for 10 iterations and STOP button on browser is clicked, the output file shows 13 iterations with ~ 13.01 seconds taken.

    2) When code is run for 20 iterations and STOP button on browser is clicked, the output file shows 23 iterations with ~ 23.01 seconds taken.


    Inferences & Conclusion:

    1) The script actually does NOT stops when the STOP button is clicked but after 2-4 seconds of clicking it. So, there are more iterations that what appears in the browser.

    2) The number of iterations is SAME as the number of seconds it takes to execute, as shown in output file.

    Therefore, there is no error and apparently no bugs, it’s just the latency time between clicking the STOP button and the script actually stopping.


    Notes:

    1) Server: A Linux VPS.
    2) Clients tested: Firefox and Chrome.
    3) As the script ends 2-4 seconds after STOP is clicked, it takes around 3-4 seconds for the output file to be updated for the current test.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can anyone tell me how I can display a status message like 12 seconds
Can anyone tell me if there are RDBMSs that allow me to create a
Can anyone tell me what the need/advantage is to using a web service with
Can anyone tell me what is the memory usage overhead associated with PHP opcode
Can anyone tell me what exactly does this Java code do? SecureRandom random =
Can anyone tell me what version of the .NET framework (CLR and BCL) is
Can anyone tell me how can I get the xpath of the name attribute
Can anyone tell me where the config of cache folder is for phpbb3? I
Can anyone tell me why the second cast fails to compile in Delphi 7?
Can anyone tell me how to add an AssemblyInfo.vb file to a Web Application

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.