Page 1 of 1
slowwwwwwwww
Posted: Sat Mar 04, 2006 6:26 pm
by s.dot
this function i have,
Code: Select all
return substr(sha1(uniqid(1)),0,10));
is slowww. It takes roughly a 10th of a second just to generate that. My page load times were under 5 thousandths of a second until i add that, then they jump up to .015 seconds. And.. well i want my page load times to look extremely cool
Is there a way to acheive the same affect using faster functions?
Posted: Sat Mar 04, 2006 6:29 pm
by s.dot
hmm, testing it locally I get 0.00006 seconds.
maybe it's not that function, but something else in my class. =/ i'll get back to you
[edit]
some test results:
without that function: Page executed in 0.00424 seconds
with that function: Page executed in 0.02151 seconds
[edit # 2]
using this function
Code: Select all
return substr(md5(microtime()),0,10);
It executes in 0.00528 seconds.
So this appears to be MUCH faster. Should it still produce as random of a string as the first function?
Posted: Sat Mar 04, 2006 6:45 pm
by josh
It's likely the sha1 hash, try md5
Edit:
guess you edited your post, anyways its hard to say since you're truncating it to 10 characters anways... The idea isn't to have it random from what I infer from your code but to generate a unique string. If having sequential numbers isn't a problem that's how I'd go. If you want to increase entropy obviously don't truncate to the 10th character. It's still always possible to have a collision no matter what hashing function you are using so you will always need to have some kind of check. Just out of curiosity what is this being used for? A sessionID, a token in a form?
Posted: Sat Mar 04, 2006 6:47 pm
by s.dot
I switched that up and got roughly the same time, so I replaced uniqid(1) with microtime() and it reduced the time a lot
Posted: Sun Mar 05, 2006 12:39 pm
by Weirdan
PHP Source code wrote:
Code: Select all
// from the middle of PHP_FUNCTION(uniqid)
//.......
#if HAVE_USLEEP && !defined(PHP_WIN32)
if (!MORE_ENTROPY) {
usleep(1);
}
#endif
//.......
In plain English:
on non-win32 systems uniqid sleeps for 1 microsecond if you call it with second argument set to
false (or omit it entirely)