Jump to content

Big Function, Arrays, Variables...I want to speed things up.


derrickc

Recommended Posts

Okay I have a function that stores data into an Array.  The function takes about 7 seconds to run and through a number of different loops it creates one final array with about 15,000 keys.  I want to recall this data a number of times in different functions, however how can I have this data easily accessible without running the function each time.

 

EX:

function theFunction()
{
   for($x=0;$x,=15000;$x++)
   {
   //Run the loop and store data.
   $string[$x] = //output from other loops and calculations
   }
   return $string
}

//Then later on if I want to recall the data the only way I know how is to do the follow:
$newstring = theFunction()

 

The only problem I have with this is that it has to re-run the function every time in order to get to the data it spits out.  How can I store this data into another array outside of the function without having to re-run it?

 

I hope this makes sense.

 

Thanks.

Link to comment
Share on other sites

I am using MySQL and multiple parts of the function gets data from this database.

 

I would prefer to not store the output into a database for the sake of the size.

 

I have thought about storing into a db during the session then deleting it once the session is over, but I would prefer not to as well.

Link to comment
Share on other sites

Why are you dealing with 15,000 records at a time if you're using a database system?

 

You're probably better off performing these calculations in the query itself.

 

You'll have to provide more details if you want a more specific solution.

Link to comment
Share on other sites

Fair response...

 

I'm not using 15000 records in the db, Im using 3 different tables in a db and in each table there are 3-6 rows with about 5 columns of data each.

 

These entries are days of the month a transaction occurs and the frequency in which it occurs(monthly,quarterly,etc...)

 

In the function, I am starting from today and going out x number of years on a daily basis for example 15000 keys would be roughly 41 years.

 

for( calendar date = 0 to 15000)

 

$string[calendar date] = basically my function now calls the sql db and if the calendar date matches the day the transaction is to occur it will grab the transaction amount and store it in this string.  However, in the db there isn't future dates per say.  The record shows the day of the month it occurs and if it is quarterly, then within the function I calculate those future dates and if those calculations match the calendar date it will store the data.(To be precise)

 

end loop.

 

To give you an idea of the output:

Array
(
    [12/09/2011] => 0
    [12/10/2011] => 120
    [12/11/2011] => 0
    [12/12/2011] => 0
    [12/13/2011] => 0
    [12/14/2011] => 0
    [12/15/2011] => -2000
    [12/16/2011] => 500
    [12/17/2011] => 0
    [12/18/2011] => 0
    [12/19/2011] => 0
    [12/20/2011] => 0
    [12/21/2011] => 0
    [12/22/2011] => 0
    [12/23/2011] => 0
    [12/24/2011] => 0
    [12/25/2011] => 0
    [12/26/2011] => 0
    [12/27/2011] => 0
    [12/28/2011] => 555
    [12/29/2011] => 0
    [12/30/2011] => -2000
    [12/31/2011] => -4450
    [01/01/2012] => 3636.37
    [01/02/2012] => 0
.....

 

all of this data I would like in simple array without calling the function every time.

Link to comment
Share on other sites

From what you've shown me, all I can see is random numbers assigned to a date. I don't know how this relates to the data in the database.

 

Why does all this data need to be temporarily stored? Why can't you simply use math to determine what any given value will be at any given time period? From what I kind of gather, this can be done in an easier way. You seem to be storing a lot of empty data, which doesn't need to exist in the first place.

 

You're asking a question that can only be solved by someone who knows what the input will be (sample data), what the output needs to be (sample results), and how the data will be used (be specific).

Link to comment
Share on other sites

You seem to be storing a lot of empty data

 

+1 (you should only store data that has an actual non-zero value.)

 

The reason we always ask what exactly someone is doing, is because in programming, that is the best way of getting the quickest solution to a problem, without a lot of - well try this, no try that... replies.

 

Several things you have shown or mention indicate a data/code design that you should optimize before you worry about using the array that the function returns multiple times on a page (or perhaps you actually mean across more than one page request since you also mentioned the session.)

 

I suspect that your code can be made at least 5 times faster (and if you are performing a query in a loop, probably 10 times faster) and by knowing if you actually need the same resultant data available across more than one page request, someone can also suggest ways of storing and retrieving that amount of data. Also by showing exactly (code, sample data, result) what you are doing, some kind person here might actually test the suggestions they post so that you will know that the code will work for what you are doing.

Link to comment
Share on other sites

I think we may be missing the point here.

 

The printed array is to give you a glimpse of some output and in my final code the zeros are filled in with actual values.

 

I could show you the whole code but it is irrelevant to my question.  I tried prefacing my question with a general summary of the different commands within my function.

 

I merely want the output of the function to be stored in variable.  Then I want to call that variable for its data without having to always run the function to get the data.

 

For example if I do this:

 

$x = function("blah");//lets say the output to this function is 47.5.  So now $x will always equal 47.5, but every time I use $x in my code later on, it will have to run the function again to get 47.5.

 

Im looking for a method to be able to store the output into a variable or an array so that it  can be accessed later without running the function.

 

Another example:  In excel you can have a formula spit out some value, you can then copy that cell(where the formula is) and paste special only the value.  From here on out you have a value to reference without the need of the formula. 

 

Is something like this possible in PHP without using a database?

 

Hope this clears up my question.  Thanks.

Link to comment
Share on other sites

What?

 

$x will only store the return value of the function, not the function itself. Calling $x later will simply return the array previously returned by the function, not execute the function again.

 

Regardless, there's no way you'll ever need to display 15,000 items at a time. You should allow MySQL to do the brunt of your calculations.

Link to comment
Share on other sites

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.