[php] Timer Class

Gestart doorRik15 reacties83 weergaven

16 berichten · pagina 1 van 2

Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Heya,

Alweer een class, copyright graag laten staan. Donaties zijn altijd welkom

[COLOR= #0000BB]<?php[/COLOR]

    [COLOR= #FF8000]/**
     * Timer Class
     * Counts the time needed to load the page
     *
     * @author      Rik S.
     * @copyright   Copyright (c) Rik S. All rights reserved.
     */[/COLOR]
     
[URL="http://www.php.net/class"]class[/URL] timer
[COLOR= #007700]{[/COLOR]
    [COLOR= #FF8000]/**
    * Holds the starting time
    * 
    * @var int
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$intStart[/COLOR][COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Holds the stopping time
    * 
    * @var int
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #0000BB]$intStop[/COLOR][COLOR= #007700];[/COLOR]
    
    [COLOR= #FF8000]/**
    * Start the timer 
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] startTime[COLOR= #007700]()
    {[/COLOR]
        [COLOR= #0000BB]$tempTime[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000] [/COLOR][COLOR= #007700],[/COLOR] [URL="http://www.php.net/microtime"]microtime[/URL][COLOR= #007700]());[/COLOR]
        self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$intStart[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$tempTime[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]] +[/COLOR] [COLOR= #0000BB]$tempTime[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]];     
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Stop the timer
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] stopTime[COLOR= #007700]()
    {[/COLOR]
        [COLOR= #0000BB]$tempTime[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/explode"]explode[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000] [/COLOR][COLOR= #007700],[/COLOR] [URL="http://www.php.net/microtime"]microtime[/URL][COLOR= #007700]());[/COLOR]
        self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$intStop[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]$tempTime[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]0[/COLOR][COLOR= #007700]] +[/COLOR] [COLOR= #0000BB]$tempTime[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]1[/COLOR][COLOR= #007700]];    
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Show how much time the script needed to execute, rounded by 4 digits.
    * 
    * @param int $intRound
    * @return int
    */[/COLOR]
    [URL="http://www.php.net/public"]public[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] showTime[COLOR= #007700]([/COLOR][COLOR= #0000BB]$intRound[/COLOR] [COLOR= #007700]=[/COLOR] [COLOR= #0000BB]4[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/if"]if[/URL] [COLOR= #007700]([/COLOR][URL="http://www.php.net/empty"]empty[/URL][COLOR= #007700]([/COLOR]self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$intStop[/COLOR][COLOR= #007700]))
        {[/COLOR]
            [COLOR= #0000BB]$this[/COLOR][COLOR= #007700]->[/COLOR]stopTime[COLOR= #007700]();
        }[/COLOR]
        [COLOR= #0000BB]$tempTime[/COLOR] [COLOR= #007700]=[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$intStop[/COLOR] [COLOR= #007700]-[/COLOR] self[COLOR= #007700]:[/COLOR][COLOR= #007700]:[/COLOR][COLOR= #0000BB]$intStart[/COLOR][COLOR= #007700];[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] self[COLOR= #007700]::[/COLOR]roundTime[COLOR= #007700]([/COLOR][COLOR= #0000BB]$tempTime[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$intRound[/COLOR][COLOR= #007700]);
    }[/COLOR]
    
    [COLOR= #FF8000]/**
    * Rounds the time to the given number of digits
    * 
    * @param int $intTime
    * @param int $intRound
    * @return int
    */[/COLOR]
    [URL="http://www.php.net/private"]private[/URL] [URL="http://www.php.net/static"]static[/URL] [COLOR= #007700]function[/COLOR] roundTime[COLOR= #007700]([/COLOR][COLOR= #0000BB]$intTime[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$intRound[/COLOR][COLOR= #007700])
    {[/COLOR]
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/round"]round[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$intTime[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$intRound[/COLOR][COLOR= #007700]);
    }
    
}[/COLOR]
?>


Gebruik:

[COLOR= #0000BB]<?php[/COLOR]
Timer[COLOR= #007700]:[/COLOR]SMILEYYYYYYYYYYYBEGIN[COLOR= #007700]:[/COLOR]startTime[COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/sleep"]sleep[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]2[/COLOR][COLOR= #007700]);[/COLOR]

Timer[COLOR= #007700]:[/COLOR]SMILEYYYYYYYYYYYBEGIN[COLOR= #007700]:[/COLOR]stopTime[COLOR= #007700]();[/COLOR]

[URL="http://www.php.net/echo"]echo[/URL] [COLOR= #DD0000] Script took [/COLOR] [COLOR= #007700].[/COLOR] Timer[COLOR= #007700]:[/COLOR]SMILEYYYYYYYYYYYBEGIN[COLOR= #007700]:[/COLOR]showTime[COLOR= #007700]() .[/COLOR] [COLOR= #DD0000] seconds to load[/COLOR][COLOR= #007700];[/COLOR]
?>
0
Tom

Lid

Berichten
3.868
Reputatie
0
Punten
0,02
Lid sinds
Laatst gezien
Meet die de laad tijd van een website op?
0
Berichten
93.699
Reputatie
0
Punten
1,27
Lid sinds
Laatst gezien
van een pagina ja
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Hij meet hoe lang het script nodig heeft om uitgevoerd te worden ja.
0
Tom

Lid

Berichten
3.868
Reputatie
0
Punten
0,02
Lid sinds
Laatst gezien
Wat heb je er dan aan?
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Kan je zien wat probleempaginas op je site zijn. One2xs heeft volgens mij ook een (voor ons onzichtbare) page-load logger.
0
Berichten
18.482
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Waarom microtime() exploden? Doe dan microtime(true);
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Jij had volgensmij een mysql class gemaakt. Heb je die nog?
0
Berichten
20.364
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Dankjewel, en ik kom eraan.
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Luukvdo schreef:
Waarom microtime() exploden? Doe dan microtime(true);
Maak opzich niet zoveel uit, en ik las (toen ik dit aan het maken was) op andere sites ook van exploden van microtime.
0
Berichten
18.482
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Nah, soms krijg je met alleen microtime() een raar getal. Met microtime(true) niet
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ach ik heb er nooit problemen mee gehad. Behalve dat hij (door rounden) aangeeft dat de page in 0 seconden geladen werd.
0
Berichten
57
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Handig smile.ff probere
0
Berichten
57
Reputatie
0
Punten
0,00
Lid sinds
Laatst gezien
Handig smile.ff probere
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren