Skip to content

Instantly share code, notes, and snippets.

@kavinyao
Created May 21, 2012 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavinyao/2761972 to your computer and use it in GitHub Desktop.
Save kavinyao/2761972 to your computer and use it in GitHub Desktop.
PHP: mt_rand vs rand
<?php
$range_count = array_fill(0, 100, 0);
$start_time = microtime(true);
for($i = 0;$i < 1000000;++$i) {
$randint = mt_rand(0, 999);
$range_count[$randint/10]++;
}
$end_time = microtime(true);
$duration1 = $end_time - $start_time;
$range_count2 = array_fill(0, 100, 0);
$start_time = microtime(true);
for($i = 0;$i < 1000000;++$i) {
$randint = rand(0, 999);
$range_count2[$randint/10]++;
}
$end_time = microtime(true);
$duration2 = $end_time - $start_time;
?>
<!doctype html>
<html>
<head>
<script type="text/javascript" src="js/raphael-min.js"></script>
<script type="text/javascript" src="js/g.raphael-min.js"></script>
<script type="text/javascript" src="js/g.bar-min.js"></script>
<script type="text/javascript">
window.onload = function() {
var txtattr = {font: "12px sans-serif"};
var r = Raphael("holder");
r.text(350, 10, "mt_rand: 1,000,000 numbers/range:0-999/interval:10/time:<? echo $duration1.'s'; ?>").attr(txtattr);
r.barchart(10, 10, 800, 500, [[<? echo $range_count[0];for($i=1;$i<100;++$i) echo ",{$range_count[$i]}"; ?>]]);
var r2 = Raphael("holder2");
r2.text(350, 10, "rand: 1,000,000 numbers/range:0-999/interval:10/time:<? echo $duration2.'s'; ?>").attr(txtattr);
r2.barchart(10, 10, 800, 500, [[<? echo $range_count2[0];for($i=1;$i<100;++$i) echo ",{$range_count2[$i]}"; ?>]]);
};
</script>
</head>
<body style="width:900px;margin:0 auto;">
<div id="holder"></div>
<div id="holder2"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment