Optimizing Library Load Time - are GLOBALS arrays slow?
Moderator: General Moderators
Optimizing Library Load Time - are GLOBALS arrays slow?
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.
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?
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.
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?
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.
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.Also if you're using php 5.3 you can use class constants
-
HavokDelta6
- Forum Newbie
- Posts: 16
- Joined: Mon Sep 28, 2009 4:11 pm
Re: Optimizing Library Load Time - are GLOBALS arrays slow?
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 6:01 am, edited 1 time in total.
Re: Optimizing Library Load Time - are GLOBALS arrays slow?
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.)
(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
aaaaaaaaaaaaaaaaaaaaaaaaaaa
Last edited by HavokDelta6 on Tue Mar 20, 2012 5:59 am, edited 1 time in total.
Re: Optimizing Library Load Time - are GLOBALS arrays slow?
>> but you will get immediate benefits from installing a byte-code cache such as APC
Doesn't work with suPHP.
Doesn't work with suPHP.
Re: Optimizing Library Load Time - are GLOBALS arrays slow?
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?
sorry for false information. read something recently that said class constants were introduced in 5.3
- 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?
If accessing variables is slowing you down then you problems bigger than you think!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.)
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)