Link to home
Start Free TrialLog in
Avatar of worldofwires
worldofwiresFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Reducing memory consumption

I'm trying to reduce the load on a PHP system (runnning on a Linux Apache) and as an example, I'd like to enquire about the differences between multiple date() calls as opposed to a single call and exploding the result.

This is quite a trivial example but I think I need to start small, any help and information is appreciated.

From my perspective, the use of multiple variables instead of an array is a obvious drain on resources, but is it significant? I'd assume that arrays require more memory, but where is the cut-off/threshold?
$hr=date("H");
  $dy=date("D");
  $t_dy=date("d");
  $m_dy=date("t");
Or:
  $ex=explode(":", date("H:D:d:t));

Open in new window

Avatar of worldofwires
worldofwires
Flag of United Kingdom of Great Britain and Northern Ireland image

ASKER

Sorry, I didn't close the double quotes on line 6 of example, but I'm sure you get the gist.
Lots of layers to this answer, with the first being, "What's our time worth?"
If you want to reduce the amount of storage used, you can unset() the larger variables - very long arrays, or large objects, or movie files that are dragged into storage.  However it makes no sense to unset them if you need to use them again.
SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Ray, Your 2nd and 3rd comments answer my question sufficiently.
The first however moves me to wonder if I've misunderstood the premise of this site... On top of the subscription, am I to pay for expert advice? Or did I misunderstand your comment?
Now back to the "what's our time worth" question.  PHP can run a lot of code in the time it takes me to debug an awkwardly constructed line of program code.  Unbalanced parentheses, etc, all contribute to time spent on non-productive work.  So I opt for the simplest data constructs that make my code readable and easy to understand/debug.

Hope that helps put it in perspective, ~Ray
No, you do not misunderstand the EE site at all.  What I am referring to is the time you and I spend coding and debugging.  Are we sacrificing our programming time to make PHP more efficient?  Sometimes that will make sense, if you are taking 10,000 hits per second, for example.  That is code you want to optimize!  Most of the time, however, the greatest efficiencies I can achieve are in my own work - how can I do this the fastest and most dependable way.

Does that clear it up?

Best regards, ~Ray
Thanks Ray, Very clear thank you and a very good point too, one which I hadn't really considered before.

The code in question is simply a cron which executes every minute of the working day and as such I don't want it to demand any more memory than absolutely necesssary. And with that, I shall get to work! Thank you very much indeed.
Thanks for the points.  It's a very good question.

A cron job that runs every minute around the clock runs 1440 times a day.  That is like ZERO load.  If you run it every second of every day, you're looking at 86,400 times per day.

The overhead of a PHP script, including sessions and data base connectivity (on my servers, at least) comes to a bit less than 1 millisecond, so 86,400 hits to the script uses around a minute of the 24 hour day.

As you add processing load to the overhead, you can easily compute the impact.  Just grab the microtime() at the start of the script and at the end, and subtract.  You can print out the time the script took.

Best of luck with it, over and out, ~Ray