Optimizing Library Load Time - are GLOBALS arrays slow?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

Optimizing Library Load Time - are GLOBALS arrays slow?

Post 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.
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

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

Post 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.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post 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.
HavokDelta6
Forum Newbie
Posts: 16
Joined: Mon Sep 28, 2009 4:11 pm

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

Post by HavokDelta6 »

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 6:01 am, edited 1 time in total.
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

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

Post 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.)
HavokDelta6
Forum Newbie
Posts: 16
Joined: Mon Sep 28, 2009 4:11 pm

Post by HavokDelta6 »

aaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 5:59 am, edited 1 time in total.
akreider
Forum Commoner
Posts: 46
Joined: Mon May 22, 2006 3:54 pm
Location: USA

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

Post by akreider »

>> but you will get immediate benefits from installing a byte-code cache such as APC

Doesn't work with suPHP.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

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

Post by Eran »

Missed that one. Why are you using suPHP? are you on shared hosting?
peterjwest
Forum Commoner
Posts: 63
Joined: Tue Aug 04, 2009 1:06 pm

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

Post by peterjwest »

sorry for false information. read something recently that said class constants were introduced in 5.3
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

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

Post 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.
(#10850)
Post Reply