Return to Snippet

Revision: 43506
at March 25, 2011 06:13 by Brandon0


Updated Code
<?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

$timer = microtime(true);
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


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
for ($i=0; $i<1000000; $i++) {
	$uid = substr(str_shuffle($chars), 0, 3);
#	echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using str_shuffle()' . "\n"; # 1.9944579601288 seconds


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
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

Revision: 43505
at March 25, 2011 06:12 by Brandon0


Updated Code
<?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

$timer = microtime(true);
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


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
for ($i=0; $i<1000000; $i++) {
	$uid = substr(str_shuffle($chars), 0, 3);
#	echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using str_shuffle()' . "\n"; # 1.9944579601288 seconds


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
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

Revision: 43504
at March 25, 2011 06:04 by Brandon0


Initial Code
<?php

header('Content-Type: text/plain');

$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';

$timer = microtime(true);
for ($i=0; $i<1000000; $i++) {
	$uid = $chars[rand(0, strlen($chars)-1)] . $chars[rand(0, strlen($chars)-1)] . $chars[rand(0, strlen($chars)-1)];
#	echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using rand(0, strlen()-1)' . "\n"; # 2.3072600364685 seconds


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
for ($i=0; $i<1000000; $i++) {
	$uid = substr(str_shuffle($chars), 0, 3);
#	echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using str_shuffle()' . "\n"; # 2.0080540180206 seconds


echo "\n" . str_repeat('-', 80) . "\n\n";


$timer = microtime(true);
for ($i=0; $i<1000000; $i++) {
	$uid = $chars[rand() % strlen($chars)] . $chars[rand() % strlen($chars)] . $chars[rand() % strlen($chars)];
#	echo $uid . "\n";
}
echo microtime(true) - $timer . ' seconds using mod strlen()' . "\n"; # 1.7787578105927 seconds

Initial URL

                                

Initial Description
Testing a couple different ways to create a random string 3 characters long [a-zA-Z0-9]. Turns out mod is pretty quick!

Initial Title
Random string generator

Initial Tags

                                

Initial Language
PHP