Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/71560-super-advanced-max-array-function/
Share on other sites

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?

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?

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

 

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?

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).

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.

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?

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...

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.