[PHP] Timer Class (oud)

Gestart doorRik2 reacties47 weergaven

3 berichten · pagina 1 van 1

Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Hi!

Deze vond ik net nog op mijn host, en ik doe er zelf niks mee. Misschien heeft iemand er iets aan.
De class is niet compleet, en werkt niet optimaal, zelf knutselen zal dus waarschijnlijk wel gewenst zijn.
Ik geef om die reden ook eigenlijk geen support voor de code. Vragen mag altijd uiteraard.

De class
Spoiler tonen
Codeblok (timer.cls.php):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90



/**
* @author Rik Somers
* @copyright 2012
*/


class Timer {

private static $arrLogStart = array();
private static $arrUpdates = array();
private static $arrLogStop = array();
private static $arrLogErrors = array();
private static $intStart;
private static $intStop;

public static function startTimer() {
self::$intStart = microtime(true);
}


public static function stopTimer() {
self::$intStop = microtime(true);
}


public static function addEvent($strKey, $strEvent = NULL) {
if(!isset(self::$intStart)) {
self::logError([ . microtime(true) . ] Timer class not started. Starting now. );
self:SMILEYYYYYYYYYYYBEGIN:startTimer();
}


self::$arrLogStart[$strKey] = microtime(true);
}


public static function updateEvent() {
// TODO: Add update functionality for Timer.

}

public static function stopEvent($strKey, $strEvent = NULL) {
if(array_key_exists($strKey, self::$arrLogStart)) {
self::$arrLogStop[$strKey] = microtime(true);
}

else {
self::logError(Event { . $strKey . } never started.);
}
}


private static function logError($strErrMessage) {
self::$arrLogErrors[$strErrMessage] = microtime(true);
}


public static function showTimer($strEvent = NULL) {
$strReturn = ;

if($strEvent === NULL)
{

foreach(self::$arrLogStart as $strKey => $intTime) {
if(array_key_exists($strKey, self::$arrLogStop)) {
$strReturn .= [ . $strKey . ] . (self::$arrLogStop[$strKey] - $intTime) . s [ . (self::$arrLogStop[$strKey] - self::$intStart) . s since start]
;
}
}
}

else
{
if(array_key_exists($strEvent, self::$arrLogStart)) {
if(array_key_exists($strEvent, self::$arrLogStop)) {
$strReturn .= [ . $strEvent . ] . (self::$arrLogStop[$strEvent] - $intTime) . s [ . (self::$arrLogStop[$strEvent] - self::$intStart) . s since start]
;
}
}
}


return $strReturn;
}


public static function showTimerErrors() {
if(count(self::$arrLogErrors) != 0) {
$strReturn = ;
foreach(self::$arrLogErrors as $strKey => $intTime) {
$strReturn .= $strKey . [ . (self::$arrLogErrors[$strKey] - self::$intStart) . s since start]
;
}

return $strReturn;
}

else {
return No errors occured.;
}
}

}


?>

Voorbeeld
Spoiler tonen
Codeblok (voorbeeld.php):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39



/**
* @author Rik Somers
* @copyright 2012
*/


include(app/inc/bootstrap.php); // bootstrap heeft een autoloader. Gebruik je eigen autoloader of laadt de class handmatig in!

Timer:SMILEYYYYYYYYYYYBEGIN:startTimer();

Timer::addEvent(Executing useless for);

for($i=0; $i < 1000000; $i++) {
// Do nothing lol
}

Timer:SMILEYYYYYYYYYYYBEGIN:stopEvent(Executing useless for);

Timer::addEvent(Executing useless for 2);

for($i=0; $i < 1000000; $i++) {
// Do nothing lol
}

Timer:SMILEYYYYYYYYYYYBEGIN:stopEvent(Executing useless for 2);

Timer::addEvent(Executing useless for 3);

for($i=0; $i < 1000000; $i++) {
// Do nothing lol
}

Timer:SMILEYYYYYYYYYYYBEGIN:stopEvent(Executing useless for 3);

echo Timer:SMILEYYYYYYYYYYYBEGIN:showTimer();

?>

- Rik
0
Berichten
14.025
Reputatie
1
Punten
0,01
Lid sinds
Laatst gezien
Codeblok (voorbeeld.php):

1
2
3

for($i=0; $i < 1000000; $i++) {
// Do nothing lol
}

???
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Dylanh schreef:
Codeblok (voorbeeld.php):
1
2
3

for($i=0; $i < 1000000; $i++) {
// Do nothing lol
}

???

Quote:
Executing useless for

Het is een voorbeeldscript om te laten zien hoelang een loop van 1 miljoen iteraties duurt.

zie http://www.weprogram.it
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