Skip to content

Instantly share code, notes, and snippets.

@Miguel-Serejo
Created August 28, 2018 14:13
Show Gist options
  • Save Miguel-Serejo/3210563364367f29cf87bec5de49647d to your computer and use it in GitHub Desktop.
Save Miguel-Serejo/3210563364367f29cf87bec5de49647d to your computer and use it in GitHub Desktop.
<?php
set_time_limit(0);
function intWithStyle($n)
{
if ($n < 1000) return $n;
$suffix = ['','k','M','G','T','P','E','Z','Y'];
$zeros = strlen($n)/3;
$power = is_float($zeros) ? (int) $zeros : $zeros - 1;
return round($n/(1000**$power),3).$suffix[$power];
};
function floorLogRound ($n)
{
if ($n < 1000) return $n;
$suffix = ['','k','M','G','T','P','E','Z','Y'];
$power = floor(log($n, 1000));
return round($n/(1000**$power),3).$suffix[$power];
};
function floorLogFormat($n) {
if ($n < 1000) return $n;
$suffix = ['','k','M','G','T','P','E','Z','Y'];
$power = floor(log($n, 1000));
return number_format($n/(1000**$power),3).$suffix[$power];
}
$max_exponent = 26;
$ns = [];
for($i = 0; $i <= $max_exponent; $i++) {
$ns[] = 10**$i;
}
$runs = 100000;
$total_intWithStyle = 0;
$total_floorLogRound = 0;
$total_floorLogFormat = 0;
$fastest_intWithStyle = INF;
$fastest_floorLogRound = INF;
$fastest_floorLogFormat = INF;
$slowest_intWithStyle = 0;
$slowest_floorLogRound = 0;
$slowest_floorLogFormat = 0;
for ($i=0; $i < $runs; $i++) {
$start_intWithStyle = microtime(true);
foreach($ns as $n){
intWithStyle($n);
}
$end_intWithStyle = microtime(true);
$time_intWithStyle = $end_intWithStyle - $start_intWithStyle;
if ($time_intWithStyle > $slowest_intWithStyle) {
$slowest_intWithStyle = $time_intWithStyle;
}
if ($time_intWithStyle < $fastest_intWithStyle) {
$fastest_intWithStyle = $time_intWithStyle;
}
$total_intWithStyle += $time_intWithStyle;
$start_floorLogRound = microtime(true);
foreach($ns as $n)
floorLogRound($n);
$end_floorLogRound = microtime(true);
$time_floorLogRound = $end_floorLogRound - $start_floorLogRound;
if ($time_floorLogRound > $slowest_floorLogRound) {
$slowest_floorLogRound = $time_floorLogRound;
}
if ($time_floorLogRound < $fastest_floorLogRound) {
$fastest_floorLogRound = $time_floorLogRound;
}
$total_floorLogRound += $time_floorLogRound;
$start_floorLogFormat = microtime(true);
foreach($ns as $n)
floorLogFormat($n);
$end_floorLogFormat = microtime(true);
$time_floorLogFormat = $end_floorLogFormat - $start_floorLogFormat;
if ($time_floorLogFormat > $slowest_floorLogFormat) {
$slowest_floorLogFormat = $time_floorLogFormat;
}
if ($time_floorLogFormat < $fastest_floorLogFormat) {
$fastest_floorLogFormat = $time_floorLogFormat;
}
$total_floorLogFormat += $time_floorLogFormat;
}
?>
<table>
<thead>
<th></th>
<th>intWithStyle</th>
<th>floorLogRound</th>
<th>floorLogFormat</th>
</head>
<tbody>
<tr>
<th>Fastest</th>
<td><?= $fastest_intWithStyle ?></td>
<td><?= $fastest_floorLogRound ?></td>
<td><?= $fastest_floorLogFormat ?></td>
</tr>
<tr>
<th>Slowest</th>
<td><?= $slowest_intWithStyle ?></td>
<td><?= $slowest_floorLogRound ?></td>
<td><?= $slowest_floorLogFormat ?></td>
</tr>
<tr>
<th>Total</th>
<td><?= $total_intWithStyle ?></td>
<td><?= $total_floorLogRound ?></td>
<td><?= $total_floorLogFormat ?></td>
</tr>
<tr>
<th>Average</th>
<td><?= $total_intWithStyle / $runs?></td>
<td><?= $total_floorLogRound / $runs?></td>
<td><?= $total_floorLogFormat / $runs?></td>
</tr>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment