views:

96

answers:

3

I have asked aqbout timezones and date/time before but this is a more specific question, more about Objects in PHP.

<?PHP
//set the user's time zone on page load
date_default_timezone_set("America/Los_Angeles");

//convert the timestamp from DB into the user's time
$timestamp = '2008-05-01 13:44:19'; //this would normally be returned from mysql
echo ConvertDateTime($timestamp, "America/Los_Angeles");

//here is the function to convert it
function ConvertDateTime($timeString, $timeZone)
{
    if ( $d = new DateTime($timeString) ) {
        $d-&gt;setTimeZone(new DateTimeZone($timeZone));
        return $d-&gt;format("Y-m-d H:i:s");
    }
    return null;
}
?>

Ok this is probably a dumb queston to some but I really do not know a lot or have much experience when it comes to classes and objects, generally on a page for example I will create 1 database object and run every mysql query on the page with that 1 object set. I always think less objects is probably better but I have read on this site that having hundreds of objects would be the same speed and resourses used compared to 1 object, not sure f it's true or not.

My question, in the function above, you can see that a new DateTime object is created everytime the function is called, could be 100 times on some pages in my situation. So should I use like a singlton or even I could set a dattim object 1 time in the header where my database object is set as well or should I just let the function create a ton of object?

Please explain the pros and cons and which I should probably do, appreciate any advice, some may say I am pre-optimizing, well I have been optimizing a current site for over 2 years to get it where I wanted it and now I am re-building that SAME site so I kinda know what userload to expect and stuff and I would rather fix something like this now instead of later.

+3  A: 

should I use like a singlton

Singleton are not without problems -- for instance, they often make (automated) testing difficult. Which is one of the reasons for which they are less and liked by "big" Frameworks (I'm thinking about Zend Framework and Symfony, which are reducing the number of Singletons for their next major version)


I could set a dattim object 1 time in the header

Using a global variable / object can go with side-effects, too ; If a variable is meant as "local", it should be local -- code readibily and ability to understand it are generally more important than a fraction of a millisecond.


For extreme and specific cases, the only (true) solution is to profile : you want your script / application to go faster ? To use less CPU ?

Then, first of all, profile it ! It'll help identify bottlenecks -- and show you on what you should concentrate.

Chances are you have plenty of things that could be optimized fastest (i.e. which would take less time to think about, code, and test) -- and would get you higher performance gains.


In the end, you always have to choose between :

  • the best possible performances
  • time it takes to develop / optimize something
  • time it takes to maintain it ; including for someone else than you.

And, quite often, a new, more powerful server (or a second one) will finally coest less than a couple of weeks spent optimizing to get only 5% !

(Note I didn't say you shouldn't optimize ; but you shouldn't over-optimize.)

Pascal MARTIN
besides doing test and stuff, I guess I am trying to find out is it bad to only use 1 object? I see people using a ton of objects sometimes and I don't see a reason, I am new to objects though so there might be a reason and I just don't know what it is yet?
jasondavis
@jasondavis : if it makes more sense to use several objects than it makes using only 1, then using only one object is "bad" : what generally matters is that your code makes sense. ;; you generally really don't need to over-optimize your code : your time is more important (and costs more, when you're being paid) than 1 or 2 percents of CPU you might (or not) gain with that kind of optimization. ;; Gaining 25% of CPU in 2 days is great (an "easy" idea that often works being "cache") ;; Gaining 1 ms in 10 days is often not worth it.
Pascal MARTIN
A: 

I always think less objects is probably better but I have read on this site that having hundreds of objects would be the same speed and resourses used compared to 1 object, not sure f it's true or not.

Additional objects require additional memory space - so you can assume that path of action will have repercussion (however, they may not be noticeable/require action on your part).

you can see that a new DateTime object is created everytime the function is called, could be 100 times on some pages in my situation. So should I use like a singlton

Yes, you could use a singleton - Or cache the result for the page (Are you displaying multiple different times on the same page - or just the same computed time in many places?)

Mr-sk
Hi it is different times and caching would be nearly impossible or not worth it as it is a large social networing site, there would be thousands of caches for do that, there could actually be millions of pages
jasondavis
A: 

In this case creating a new object each time the function is called will not give that much extra memory usage: each time the function is left, the memory is freed.

If you are calling the function a lot of times with exactly the same $timeString and $timeZone, you might want to add caching of the result. If most of the calls will be unique, I wouldn't add caching: it might help you performance in speed, but storing all results, while not reusing them is a waste of memory.

Jimmy Shelter
Yes it would be different times, I pretty much trieing to find out in my example if I should do 1 object or 100 objects for 100 different times on a page
jasondavis