Random string generator
Testing a couple different ways to create a random string 3 characters long [a-zA-Z0-9]. Turns out mod is pretty quick!
Copy this code and paste it in your HTML
<?php
header('Content-Type: text/plain');
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$chars_strlen = strlen($chars);
//turns out this is fairly expensive; make sure to only do it once
for ($i=0; $i<1000000; $i++) {
$uid = $chars[rand(0, $chars_strlen-1)] . $chars[rand(0, $chars_strlen-1)] . $chars[rand(0, $chars_strlen-1)];
# echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using rand(0, strlen()-1)' . "\n";
# 1.6855049133301 seconds
for ($i=0; $i<1000000; $i++) {
# echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using str_shuffle()' . "\n";
# 1.9944579601288 seconds
for ($i=0; $i<1000000; $i++) {
$uid = $chars[rand() % $chars_strlen] . $chars[rand() % $chars_strlen] . $chars[rand() % $chars_strlen];
# echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using mod strlen()' . "\n";
# 1.0942420959473 seconds
Report this snippet