shakeitdown Posted October 2, 2007 Share Posted October 2, 2007 hi, i'm out here looking for a super fast function to find the max in an array. iterating through the entire array is just too slow. the max function won't work either because my arrays look like this: array( array(price1, item_id1, quantity1), array(price2, item_id2, quantity2), array(price3, item_id3, quantity3) ) i'm trying to find the highest price...anybody have any genius ideas? Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/ Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 http://us.php.net/manual/en/function.rsort.php have to do it for the multideminosnal part but otherwise its pretty easy Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360292 Share on other sites More sharing options...
shakeitdown Posted October 2, 2007 Author Share Posted October 2, 2007 ok, you would be right except for that i explained something wrong! my array looks like this: array(OBJECT, OBJECT, OBJECT); each OBJECT has it's own price, item_id, and quantity....can't use rsort on objects... anyone know a fast way to finding the highest price? Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360303 Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 yes you need to create a sorting function sorting the Larger exterior array based on the innear array of Price. Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360309 Share on other sites More sharing options...
shakeitdown Posted October 2, 2007 Author Share Posted October 2, 2007 yea, i kinda figured that already. i have something that works just by using a foreach loop. it's way too slow and cripples the program. i'm really looking for a fast method here, not just a general method. aren't there tricks to speed up such a function? Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360311 Share on other sites More sharing options...
freakstyle Posted October 2, 2007 Share Posted October 2, 2007 iterating over data using foreach is not a performance hit. if your application / function slows to a crawl when you add this in there, then something else should be the issue. if you are trying to loop over, lets say a gig of data, then of course you'll have a hit on performance, the question to you would be ... why are you setting so much data in the array? can you limit the count of your array? why do you have an array of objects when you would do better to have an object that holds your array. it sounds like you need to re-think your overall approach if your having issues just looping over an array. ps. its not nice to post a request for help, and then ask for someone to write you a function, you need to do the leg work and above all, show your code. you will get much more pointed help if we can see what you are doing. most developers aren't teachers and do not explain everything perfectly clear. good luck Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360322 Share on other sites More sharing options...
shakeitdown Posted October 2, 2007 Author Share Posted October 2, 2007 there is no way around having the array store as many objects as it does. the count must be as it is. i could, however, start storing my data in a multidimensional array. it is an either/or situation for me. until now i didn't give it much thought because speed wasn't an issue then. perhaps traversing a simple multidimensional array would work faster than traversing a single dimensional array storing "object arrays". perhaps a sort works faster than iterating the entire array? maybe i should port the code to a language that can compile, such as C or Java? my code is simple, ineffective speed-wise, (and doesn't include much leg work ) $largest = 0; foreach ($a as $b) { $largest = ($b->price() > $largest) ? $b->price : $largest; } if i was using a multidimensional array i would do the same except have the comparison be: $largest = ($b["price"] > $largest) ? $b["price"] : $largest; i'm wondering whether a sort function might improve on the speed? what other methods could be used? Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360331 Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 you need to first sort the price column in the inner array then get its upper level key to use that key structure to resort the higher level. I did it before and then lost the script. Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360333 Share on other sites More sharing options...
shakeitdown Posted October 2, 2007 Author Share Posted October 2, 2007 i don't really understand what you mean by inner array. i don't know how to produce it. what we have is basically: $myarray = array( array("4.95", "3928", "200"), array("5.00", "4221", "300"), array("6.00", "2319", "400") ) what i'm looking to have as a result is either the index (2) or the value (6.00). Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360340 Share on other sites More sharing options...
Psycho Posted October 2, 2007 Share Posted October 2, 2007 Sorting the inner array as cooldude has suggested may be faster and it may not. You would use array_multisort() for that: http://us2.php.net/manual/en/function.array-multisort.php But, you can get about a 20% performance gain by making the following change: Change this $largest = ($b->price() > $largest) ? $b->price : $largest; To this: if ($b->price() > $largest) { $largest = $b->price(); } Although the first piece of code is more compact and elegant it is not more efficient because it is resetting the value of $largest each and every time, whereas the 2nd piece of code only resets the value when needed. I did some tests and found that the 2nd set of code completed in about 20% less the time the first operation took. However, I would suspect that you are creating this large array somehow. When you create the array you could also determine the max value at the same time. Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360350 Share on other sites More sharing options...
cooldude832 Posted October 2, 2007 Share Posted October 2, 2007 I'm going to rewrite this so I can understand it and hopfulyl it helps you also <?php $items = array(); $items[$item_number]['price'] = "6.00"; $items[$item_number]['Description'] = "Nice"; $items[$item_number2]['price'] = "3.00"; $items[$item_number2]['Description'] = "bad"; $items[$item_number3]['price'] = "5.00"; $items[$item_number3]['Description'] = "Nice and good"; ?> So you will be sorting b y $items[anyitemnumber]['price'] and then use those keys to restructure the outter array $items[] making more sense? Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360352 Share on other sites More sharing options...
shakeitdown Posted October 2, 2007 Author Share Posted October 2, 2007 so you think the absolute fastest way to get only the highest price is by doing this (based on your array above)? $largest = 0; foreach ($items as $item) { if ($item["price"] > $largest) { $largest = $item["price"]; } } echo $largest; also, what if the array looked like this instead: $items = array(); $items[$item_number]['price'] = "6.00"; $items[$item_number]['quantity'] = "500"; $items[$item_number2]['price'] = "6.00"; $items[$item_number2]['quantity'] = "400"; $items[$item_number3]['price'] = "5.00"; $items[$item_number3]['quantity'] = "100"; How would you write the function to return the item_#2 (since it's 'better' than item_#1)... this is such a pain in the butt because the array changes based on a stream of data. moreover, milliseconds matter... Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-360356 Share on other sites More sharing options...
Psycho Posted October 3, 2007 Share Posted October 3, 2007 How are you creating these arrays? As I stated before you could probably find the max at the time you are creating the arrays. Quote Link to comment https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/#findComment-361057 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.