Skip to content

Commit 3340b8a

Browse files
author
SmetDenis
committed
Timer::formatMS() added
1 parent 30b7769 commit 3340b8a

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/Timer.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,34 @@ class Timer
4444
*/
4545
public static function format($time)
4646
{
47-
$ms = round($time * 1000);
48-
47+
$time = round($time * 1000);
4948
foreach (self::$_times as $unit => $value) {
50-
if ($ms >= $value) {
51-
$time = floor($ms / $value * 100.0) / 100.0;
49+
if ($time >= $value) {
50+
$time = floor($time / $value * 100.0) / 100.0;
5251
return $time . ' ' . $unit . ($time == 1 ? '' : 's');
5352
}
5453
}
5554

56-
return $ms . ' ms';
55+
return $time . ' ms';
56+
}
57+
58+
/**
59+
* Formats the elapsed time as a string.
60+
*
61+
* @param float $time
62+
* @return string
63+
*/
64+
public static function formatMS($time)
65+
{
66+
$decimals = 3;
67+
68+
if ($time >= 0.001 || $time == 0) {
69+
$decimals = 0;
70+
}
71+
72+
$time *= round(1000, 3);
73+
74+
return number_format($time, $decimals, '.', ' ') . ' ms';
5775
}
5876

5977
/**

tests/TimerTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ public function testSecondsToTimeString($string, $seconds)
3535
isSame($string, Timer::format($seconds));
3636
}
3737

38+
/**
39+
* @dataProvider milliSecondsProvider
40+
* @param string $string
41+
* @param mixed $seconds
42+
*/
43+
public function testSecondsToTimeStringInMillisecond($string, $seconds)
44+
{
45+
isSame($string, Timer::formatMS($seconds));
46+
}
47+
3848
public function testGetRequestTime()
3949
{
4050
isTrue(Timer::getRequestTime());
@@ -45,6 +55,24 @@ public function testTimeSinceStart()
4555
isTrue(Timer::timeSinceStart());
4656
}
4757

58+
/**
59+
* @return array
60+
*/
61+
public function milliSecondsProvider()
62+
{
63+
return array(
64+
array('1 000 ms', 1),
65+
array('100 ms', 0.1),
66+
array('10 ms', 0.01),
67+
array('0 ms', 0),
68+
array('1 ms', 0.001),
69+
array('0.100 ms', 0.0001),
70+
array('0.010 ms', 0.00001),
71+
array('0.001 ms', 0.000001),
72+
array('0.000 ms', 0.0000001),
73+
);
74+
}
75+
4876
/**
4977
* @return array
5078
*/

0 commit comments

Comments
 (0)