2

This is my code:

$frontendOptions = array('lifeTime' => 10);
$backendOptions = array('cacheDir' => '../tmp/');    

$cache = Zend_Cache::factory(   'Core',
                                'File',
                                $frontendOptions,
                                $backendOptions);

$cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

$locale = new Zend_Locale('es_ES');
Zend_Registry::set('Zend_Locale', $locale);
Zend_Locale::setCache($cache);

$date = new Zend_Date('11-03-2010');

The script slows down when I try to create the Zend_Date object.

I'm using Zend 1.11.5 on MAMP (Mac / Snow Leopard).

Does anyone have any idea how can I speed up this?

iVela
  • 1,160
  • 1
  • 19
  • 40
  • 2
    Can you quantify "slow" a bit better? – deceze Jun 02 '11 at 02:41
  • 2
    What version of PHP are you using? PHP 5.3 has a native date object which should be faster. – Billy ONeal Jun 02 '11 at 02:42
  • quantify: 60 seconds and counting... \ my version of php is 5.3 but what I really need to use is Zend_Validate_Date, and testing I've found that the slow problem begans with Zend_Date and Zend_Locale – iVela Jun 02 '11 at 15:03

2 Answers2

5

Problem is your cache lifetime is really low. Zend_Date uses Zend_Locale, that parses some XMLs (not very small ones), which takes lots and lots of time. Create special cache instance with extremely long lifetime (or better tie it to a files' modified time) and set that to both Zend_Date and Zend_Locale. That should speedup things dramatically. But it's not a good idea to use Zend_Date for say... echoing date in orders list. You can use

date('d.m.Y', strtotime($timeFromDb))

You should use Zend_Date when doing date calculations or other advanced things with dates. Not for simple choing, unless you need abbility to echo it practically in any locale :)

Tomáš Fejfar
  • 11,129
  • 8
  • 54
  • 82
  • In really I need to use Zend_Validate_Date, and when I tried to use it, I stayed more than 60 second waiting for the script. Then debbuging i've found that the slow problem begans with Zend_Date and Zend_Locale – iVela Jun 02 '11 at 14:00
  • That's what the part about caching Locale and Date with long lifetime adresses ;) – Tomáš Fejfar Jun 03 '11 at 22:57
0

Zend_Date and a few other Zend classes are known to be slow. You can speed-up subsequent calls to Zend_Date by using your cache:

$date = new Zend_Date(...);  
 ....  
$cachedDate = new Cache($date);  
print $cachedDate->toString();

I know this will sound simplistic but if you just need a formatted regular date, just use php's built-in date object. I guarantee you will see a major speed difference.

UPDATE:

If you really want to tweak the class to improve performance, this post has a couple of patches / tweaks you can apply.

Stephane Gosselin
  • 9,030
  • 5
  • 42
  • 65
  • `The script slows down when I try to create the Zend_Date object.` --- so caching of `toString()` wouldn't help in this case (supposing what OP says is true) – zerkms Jun 02 '11 at 02:52
  • It wouldn't help on instanciation, and it says so in the post. If he is not re-using the object, php's native class would be better. If he IS re-using the object, the cache is an interesting alternative. – Stephane Gosselin Jun 02 '11 at 03:21