slowwwwwwwww

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

slowwwwwwwww

Post 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 :-D

Is there a way to acheive the same affect using faster functions?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post 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?
Last edited by josh on Sat Mar 04, 2006 6:48 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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)
Post Reply