I was reading a post here and toplay referred someone to the http://theomega.org/ for captcha info. I went to checkit out and noticed that on the bottom of the page it said:

  • Page Generated In: 0.1911 seconds.

How does one know this using PHP?

-thanks!

    Rodney H. wrote:

    I was reading a post here and toplay referred someone to the http://theomega.org/ for captcha info. I went to checkit out and noticed that on the bottom of the page it said:

    • Page Generated In: 0.1911 seconds.

    How does one know this using PHP?

    -thanks!

    By get a timestamp value using [man]microtime[/man] function
    at start of page
    and at end of page - the time for page generation is the difference between these 2 timestamp

    preferable you can use header.php and footer.php includes as the 2 timing points

    Time function can be used in this good way
    as
    like in my script in my Topic here:
    http://phpbuilder.com/board/showthread.php?t=10303366

    /halojoy
    has time sometimes
    but no time left when his life is over

    .

      halojoy wrote:

      By get a timestamp value using [man]microtime[/man] function
      at start of page
      and at end of page - the time for page generation is the difference between these 2 timestamp

      Cool. Great, Halojoy. Thanks! I went to the manual and found this script at the bottom in the user's comments:

      <?php
      /* A simpler page execution script would be the following */
      // ouput: 'Page executed in 0.5364 seconds'
      
      echo ("Page executed in ");
      $execute = microtime();
      print (number_format($execute,4));
      echo (" seconds.");
      
      ?>

      What do you think about putting this at the very bottom of the page? Seems effiecient to me. Must one take a start time and an end time? Or, is it ok to put this at the verrrrrrrrrrrry bottom of the page?? 😕

        If you look closer at the manual, you'll see all microtime() does is return a unix timestamp with microseconds. So I don't think just calling it once is going to give you an accurate page load time.

        The first few examples show exactly what you want to do, which is exactly what halojoy suggested.

          OK. Thanks to you both, gufmn and halojoy. I just wanted to clarify that. Thanks again.

            🙂

            /halojoy - nice time functions()

            <?php
            
            /////////  functions  ////////////////////
            
            // Get microtime, with microsecond precision
            //
            function mtimes(){
            return array_sum(explode(" ",microtime()));
            }
            
            // Delay $s number of seconds
            // If no value, wait 1 sec
            //
            function waitsec($s=1){
            $a=array_sum(explode(" ",microtime()));
            while(array_sum(explode(" ",microtime()))-$a<$s);
            }
            
            
            ////////////////////////////////////////  use of halojoy mtimes()  function
            ///////// 1. read start mtimes()
            ///////// 2. do something
            ///////// 3. read end mtimes()
            ///////// 4. calculate difference
            ///////// 5. display result
            
            $loops = 2000;    // number of loops, can also be set by a rand() function
            
            $beg = mtimes(); // start stopwatch timer
            for ($i=0;$i<$loops;$i++){
            //      do nothing but loop
            }
            $end = mtimes(); // stop stopwatch
            
            
            
            // Calculate difference between $end and $beg and display results
            // Round time to 1-6 decimals
            
            $timewas = $end - $beg;
            
            // Round to 5 decimals for display
            // 1 usec = 1/1.000.000 of 1 second
            // So max 6 decimals in useconds, min 1 decimal 1/10 second
            $displaytimewas = float( round( $timewas,5 ) );
            
            // Display result
            echo "$beg = $beg <br>";
            echo "$end = $end <br>";
            echo "$timewas = $timewas <br><br>";
            echo "Time was<b> $displaytimewas </b>seconds<br>";
            
            ?>

            /halojoy - nice time functions

            🙂

              halojoy, your waitsec() function could be replaced by [man]sleep/man or [man]usleep/man.

                according to what I can remember
                there are OS that cant use usleep

                anyway I am sure I remember there is a reason I did not use usleep
                there is always some reasoning inhalojoy programming
                be sure of this!
                man

                but my mtimes() function is as small and compact as can be,
                that is
                to disturb any timing as little as possible
                it is a nice way to read [man]microtime[/man] into seconds

                now
                I did not expect any encuragement
                or positive feedback from most boring memembers here
                I have learned this by now

                only jump at you, when they see a little chance of critic

                /halojoy
                is glad to know people around here, that are different and positive
                like RodneyH
                thanks mate - for your exsistence in a cold and hard world
                😉

                <?php
                
                function mtimes(){return array_sum(explode(" ",microtime()));}
                
                $time1=mtimes();
                $time2=mtimes();
                $intervall=$time2-$time1;
                echo "seconds= $intervall";
                
                ?>

                  there are OS that cant use usleep

                  According to the PHP Manual, usleep() wasnt available on MS Windows until PHP5. However, sleep() doesnt have that problem, and your waitsec() function emulates sleep(), not usleep() - so it makes sense to use sleep() rather than to implement your own version.

                  but my mtimes() function is as small and compact as can be,
                  that is
                  to disturb any timing as little as possible
                  it is a nice way to read microtime into seconds

                  This is interesting though. We use almost the same thing, except that I originally discarded the idea of using array_sum() in view that a function call would be slower than simple addition. However, it appears that the version with array_sum() is faster, so I've swapped to your version 🙂 I suspect that this is due to the need to assign the output of explode() to a variable before performing the addition.

                  now
                  I did not expect any encuragement
                  or positive feedback from most boring memembers here

                  I've learnt quite alot by 'boring' people pointing out improvements to me. You really should take this in your stride.

                    halojoy, laserlight, and gufmn,

                    I have learned a lot from all of you! Thanks!! I really appreciate this forum and the inteligent people who are willing to help us.

                    -Thanks.

                      <?php
                      
                      // Function waitsec() by halojoy
                      // Delay $s number of seconds, If no value, wait 1 sec
                      
                      function waitsec($s=1){
                      $a=array_sum(explode(" ",microtime()));
                      while(array_sum(explode(" ",microtime()))-$a<$s);
                      }
                      /////////////////////////////////////////////////////////
                      
                      // Usage
                      waitsec();     // delay 1.000 sec
                      waitsec( 5 );   // delay 5.000 sec
                      
                      // The delay is VERY exact!
                      // halojoy have tested using start - end timing with microtime()
                      
                      ?>

                      Despite I can use sleep, I will use my waitsec().
                      I think it is an elegant little creation.
                      You have already said my use of array_sum is fast. ( thanks for that laserlight )

                      My function is easy to understand = userfriendly.
                      You simply set a delay in seconds. That's it.

                      Now, I have found not much use of such a delay function in my PHP scripts.
                      I use in a delay in my file write lock function. ( flock() does not work in FAT32 systems )

                      /halojoy

                      🆒

                        Write a Reply...