Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Efficiency of Array Functions


[SOLVED] Efficiency of Array Functions

Reply
  #1 (permalink)  
Old 05-22-08, 08:12 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Question [SOLVED] Efficiency of Array Functions

Lets assume that I have the following code:

Code:
shuffle($_SESSION['useNums']);//shuffles possible questions
$randNum=array_pop($_SESSION['useNums']);//selects from shuffled list
The array useNums has a total of 7000-10000 integers (they start from 0 and go to the number of elements the array contains)

How efficient is that method? I am really worried because I expect this website to get millions of hits after it is released. I do not want to overuse the processor and get my account banned from my hosting service (I am not on a dedicated server).

On the other hand, is there a more efficient method of selecting a number from a list randomly and maintaining the fact that the number selected will be removed from the array?
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
  #2 (permalink)  
Old 05-22-08, 09:08 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$randnum $_SESSION['useNums'][rand(0,count($_SESSION['useNums'])-1)]; 

Something like that?
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #3 (permalink)  
Old 05-22-08, 09:12 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Yea, I had that before, but remember that I don't want an integer to appear twice. Even though the probability is low, the randomly selected value could be selected again.

And I am still curious about the run time efficiency of the shuffle method.
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
  #4 (permalink)  
Old 05-22-08, 09:23 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

$x=rand(0,count($_SESSION['useNums'])-1);
$randnum $_SESSION['useNums'][$x];
unset(
$_SESSION['useNums'][$x]; 
Like that then? I dont know the efficiency difference between them, but selecting a random number must really be more efficient that swapping round a huge array of numbers
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #5 (permalink)  
Old 05-22-08, 09:38 PM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
sorry, misunderstood ---
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #6 (permalink)  
Old 05-22-08, 09:51 PM
Jay6390's Avatar
Jay6390 Jay6390 is offline
Code Master
 
Join Date: Apr 2007
Location: United Kingdom
Posts: 1,330
Thanks: 0
Thanked 0 Times in 0 Posts
What?
__________________
Useful Tutorials
[ PHP Video-1-2-3 ] [ MySQL 1-2-3 ]
For any php function reference type

www.php.net/FunctionName
Reply With Quote
  #7 (permalink)  
Old 05-22-08, 10:28 PM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by nova912 View Post
sorry, misunderstood ---
Wait what did u misunderstand? Is it something that I need to clear up?

Quote:
Originally Posted by Jay6390 View Post
PHP Code:

$x=rand(0,count($_SESSION['useNums'])-1);

$randnum $_SESSION['useNums'][$x];
unset(
$_SESSION['useNums'][$x]; 
Like that then? I dont know the efficiency difference between them, but selecting a random number must really be more efficient that swapping round a huge array of numbers
Yea, I think I will go back to this method, it only makes sense that this would be faster. Though, if anyone has the run time analysis on these pieces of code, that would be really nice. Or even some clarification on how the shuffle method works and how processor consuming it is.
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
  #8 (permalink)  
Old 05-22-08, 10:50 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
Quote:
Originally Posted by sushi4664 View Post
Wait what did u misunderstand? Is it something that I need to clear up?



Yea, I think I will go back to this method, it only makes sense that this would be faster. Though, if anyone has the run time analysis on these pieces of code, that would be really nice. Or even some clarification on how the shuffle method works and how processor consuming it is.
It should be much faster because a shuffle is like a sort and sorting takes a lot of time, but the way it's written it won't work as expected. It's missing one line of code to make it work properly.

The way it's written, when a random number is picked it unsets that element.
So in the array that element will be missing.
Say for instance you have 4 elements then the count would be four.
A rand number of 2 comes up and the third element gets unset.
Now you have a count of 3.
So if a rand number of 2 comes up next the third element will not be there because it was unset.
So the remaining elements need to be merged so the indexing is correct.
Then it will work without any problems.

Like this:
PHP Code:

$rand_element=rand(0,count($_SESSION['useNums'])-1);
$randnum $_SESSION['useNums'][$rand_element];
unset(
$_SESSION['useNums'][$rand_element]);
$_SESSION['useNums']=array_merge($_SESSION['useNums']); 
__________________
Jerry Broughton

Last edited by job0107; 05-22-08 at 10:56 PM.
Reply With Quote
  #9 (permalink)  
Old 05-23-08, 05:26 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
you could also use the array_rand function instead of the rand function, that way you don't need to count the number of elements in the array first (php will probably do it for you in the array_rand function, so I don't think you'll see a lot of improvement there).
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #10 (permalink)  
Old 05-23-08, 06:15 AM
sushi4664's Avatar
sushi4664 sushi4664 is offline
Aspiring Coder
 
Join Date: Apr 2007
Location: USA
Posts: 411
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by job0107 View Post
It should be much faster because a shuffle is like a sort and sorting takes a lot of time, but the way it's written it won't work as expected. It's missing one line of code to make it work properly.

The way it's written, when a random number is picked it unsets that element.
So in the array that element will be missing.
Say for instance you have 4 elements then the count would be four.
A rand number of 2 comes up and the third element gets unset.
Now you have a count of 3.
So if a rand number of 2 comes up next the third element will not be there because it was unset.
So the remaining elements need to be merged so the indexing is correct.
Then it will work without any problems.

Like this:
PHP Code:

$rand_element=rand(0,count($_SESSION['useNums'])-1);
$randnum $_SESSION['useNums'][$rand_element];
unset(
$_SESSION['useNums'][$rand_element]);
$_SESSION['useNums']=array_merge($_SESSION['useNums']); 
Ok, this is what I was looking for. I will stick with this method.

Quote:
Originally Posted by UnrealEd View Post
you could also use the array_rand function instead of the rand function, that way you don't need to count the number of elements in the array first (php will probably do it for you in the array_rand function, so I don't think you'll see a lot of improvement there).
And, nice thinking! I hadn't thought of that.

Thanks!
__________________
- sushi
Visit http://napkinz.com/index.php - web comic that is update weekly

-ps: got through the archive...there are really funny comics in there....
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Make A New Array From An Old Array (Excluding 1 Array Element) w2n PHP 14 08-17-07 03:24 PM
Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ')' Dr. Forensics PHP 3 07-15-06 03:54 PM
Sorting a massive multidimensional array dave111 PHP 1 12-12-05 04:09 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 07:44 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.