Tuesday, May 08, 2012

When your 'mktime' is slow as shit

While working on Vanilla Forum, I realized that dashboard was taking around 3 seconds to load on LAN. When looked further using Xdebug and cachegrind log, I realized that a method called "UnixTimestamp" (in Framework.Functions.php) was calling a stupid method called "mktime". What it does is just return the timestamp value (elapsed int value from a stupid start date of C which will create problem 2030). Not sure why on the earth this should happen, but this method is utter slow. So slow that when I wrote got the UTC time from my MySQL server (of local machine), it was 3 time fast.

Conclusion:
If your mktime is slow, throw that shit out and use stupider but faster method.. i.e. tell MySQL to do it for you. Here is what I wrote:

 
  $query = sprintf("SELECT UNIX_TIMESTAMP('%s') as UTS",
     mysql_real_escape_string($DateTime));
  $result = mysql_query($query);
  if($row = mysql_fetch_assoc($result)) {
     return $row['UTS'];
  }
May PHP god have mercy on you! Happy Coding!