Solved

Reducing memory consumption

Posted on 2009-07-05
10
418 Views
Last Modified: 2012-05-07
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

0
Comment
Question by:worldofwires
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 3
10 Comments
 

Author Comment

by:worldofwires
ID: 24780676
Sorry, I didn't close the double quotes on line 6 of example, but I'm sure you get the gist.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24780680
Lots of layers to this answer, with the first being, "What's our time worth?"
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24780689
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.
0
Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 24780694
Four separate calls to a function are almost always more expensive than a single call that does the same thing (ie: returns all the data you need, such as the date("H:D:d:t"); construct above.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 24780716
For an instructive view of what's really going on in PHP, run this script.  You may find that the variables you add to the mix do not make an appreciable change in the overall amount of data that the processor has to deal with.  Note that there are only a tiny handful of variables defined in the script.  The others are all what PHP uses to set up its working environment so it can run your scripts.
<?php // RAY_get_defined_vars.php
// SHOW ALL THE VARIABLES AND CONSTANTS
error_reporting(E_ALL);
session_start();
echo "<pre>\n";
 
// MAN PAGE: http://us3.php.net/manual/en/function.get-defined-vars.php
echo "\n\n***** DEFINED VARS: ";
$data = get_defined_vars();
// unset ($data["GLOBALS"]); // OMIT THIS FOR A LOT OF OUTPUT
var_dump($data);
 
// MAN PAGE: http://us3.php.net/manual/en/function.get-defined-constants.php
// ACTIVATE THIS FOR A LOT OF OUTPUT
echo "\n\n***** DEFINED CONSTANTS: ";
var_dump(get_defined_constants());
 
// MAN PAGE: http://us3.php.net/manual/en/function.get-defined-functions.php
echo "\n\n***** DEFINED FUNCTIONS: ";
$data = get_defined_functions();
// unset ($data["internal"]); // OMIT THIS FOR A LOT OF OUTPUT
var_dump($data);
 
// EXAMPLE OF ANOTHER WAY OF GETTING THIS DATA:
/* *  *****
 * $data = get_defined_vars();
 * $data = serialize($data);
 * mail ('You@Your.org', 'VARS', $data);
*/ // *****

Open in new window

0
 

Author Comment

by:worldofwires
ID: 24780720
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?
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24780729
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
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24780740
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
0
 

Author Comment

by:worldofwires
ID: 24781168
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.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24784564
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
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

If your site has a few sections that need to be secure when data is transmitted between the server and local computer, such as a /order/ section for ordering or /customer/ which contains customer data, etc it would of course be recommended to secure…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question