Page 1 of 1

Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 2:23 pm
by akreider
I'm trying to optimize the time it takes to load the classes for my software.

It currently takes 400ms. This is especially a problem for my AJAX web services.

I am running suPHP which might make it impossible to use a php opcode cache, like APC. I'd really love to use one if it is possible!

It takes 120 ms to connect to the database. The database server is on a different computer from the webserver. Ping time between the two is around 35-40ms. I might be able to move them closer in the future, but it won't be easy.

It takes 180 ms to load my constants file. This includes a ton of GLOBALS arrays and variables.

It takes 100 ms to load my other classes.

It appears to me that defining GLOBAL arrays is slow. Is there a way I can improve this?

I could do a lot of conditional loading, but that could get very messy keeping track of in which cases I need to load which libraries. Already, my software is only 1-2mb in size.

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 4:08 pm
by peterjwest
I can't vouch for the speed of global vars. However if they are indeed slow the obvious solution is to reduce dependence on global arrays. It's not an easy task: you'll have to work out where variables are needed and pass them into the relevant classes.

Also if you're using php 5.3 you can use class constants, might be helpful.

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 4:45 pm
by Eran
This is sort of a band-aid for your situation, but you will get immediate benefits from installing a byte-code cache such as APC. A real fix would be refactoring your application to perform more efficiently.
Also if you're using php 5.3 you can use class constants
Class constants have been available ever since PHP 5 came out. You are probably talking about referring to classes using variables, which is irrelevant for this case.

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 4:48 pm
by HavokDelta6
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 5:09 pm
by akreider
It turns out that the slowness in my constants file had to do with me using the gettext library for translation. Creating the global arrays wasn't slow.

(There is evidence that iterating a global variable is twice as slow as a local variable, but for most applications this won't matter.)

Posted: Mon Sep 28, 2009 5:58 pm
by HavokDelta6
aaaaaaaaaaaaaaaaaaaaaaaaaaa

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 9:57 pm
by akreider
>> but you will get immediate benefits from installing a byte-code cache such as APC

Doesn't work with suPHP.

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Mon Sep 28, 2009 10:10 pm
by Eran
Missed that one. Why are you using suPHP? are you on shared hosting?

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Tue Sep 29, 2009 10:04 pm
by peterjwest
sorry for false information. read something recently that said class constants were introduced in 5.3

Re: Optimizing Library Load Time - are GLOBALS arrays slow?

Posted: Wed Sep 30, 2009 5:46 pm
by Christopher
akreider wrote:It turns out that the slowness in my constants file had to do with me using the gettext library for translation. Creating the global arrays wasn't slow.

(There is evidence that iterating a global variable is twice as slow as a local variable, but for most applications this won't matter.)
If accessing variables is slowing you down then you problems bigger than you think! ;) I seriously doubt that accessing globals is your problem.

Perhaps you should make the translation lazy and only call gettext when a value is requested. I doubt that you use every value in your huge array for every request for a webservice.