Fastest way to get random php number?
I'm trying to prevent caching by appending a '? t='开发者_Python百科 to the end of my JS files. What's the fastest way to get such a number? time() or rand() or something else?
time() and mt_rand() are pretty similar in terms of efficiency in PHP—you select one or the other based on what factors you need it for:
- Just hard to guess: use mt_rand() (e.g., generating use salt)
- Get a unique identifier that is hard to guess: use mt_rand(1, 931415926536); (e.g., generating session id)
- (obviously) keep records: use time() (e.g., prevent caching, logs et cetera)
If you really want to know, time() is slightly faster—but you really don't need to worry about it. (It's the difference between one or two small parts of a second.)
(mt_rand() is about 4 times as fast as rand())
You probably know this already, but be sure to always profile your code before making optimizations; often it'll run slowly for reasons completely different than what you expected.
If you're only preventing caching, time() would be sufficient.
Call me old-fashioned, but preventing caching is something that can and should be achieved by using HTTP headers, not unique URLs. If you serve the file dynamically through PHP:
<?php
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>
otherwise use a .htaccess file in apache (or similar config in any other web server):
<FilesMatch "\.js$">
Header set Cache-Control "no-cache, must-revalidate"
Header set Expires "Sat, 26 Jul 1997 05:00:00 GMT"
</FilesMatch>
Don't use rand()
, use mt_rand()
.
It uses a random number generator with known characteristics using the Mersenne Twister, which will produce random numbers four times faster than what the average libc rand() provides.
Try:
'?t=' . mt_rand(time());
this is a bit old but here is a time comparison for 10 million Execution
Total Execution Time Test mt_rand : 0.0067805965741475 Mins
Total Execution Time Test rand : 0.0068778196970622 Mins
Total Execution Time Test time : 0.0088921149571737 Mins
it looks like mt_rand is slightly faster.
$i=10000000;
$time_start_1 = microtime(true);
//test script 1
while($i--) {
mt_rand(1,100);
}
$time_end_1 = microtime(true);
$i=10000000;
$time_start_2 = microtime(true);
//test script 2
while($i--) {
rand(1,100);
}
$time_end_2 = microtime(true);
$i=10000000;
$time_start_3 = microtime(true);
//test script 3
while($i--) {
time();
}
$time_end_3 = microtime(true);
//dividing with 60 will give the execution time in minutes otherwise seconds
$execution_time_1 = ($time_end_1 - $time_start_1); // /60
$execution_time_2 = ($time_end_2 - $time_start_2); // /60
$execution_time_3 = ($time_end_3 - $time_start_3); // /60
//execution time of the script
echo '<br>';
echo '<b>Total Execution Time Test mt_rand :</b> '.($execution_time_1/60).' Mins'.'<br>';
echo '<b>Total Execution Time Test rand :</b> '.($execution_time_2/60).' Mins'.'<br>';
echo '<b>Total Execution Time Test time :</b> '.($execution_time_3/60).' Mins'.'<br>';
srand(time());
echo '?t=' . rand();
精彩评论