Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Using APC to improve PHP performance?
- Konstantin Rozinov
April 2, 2009, 3:30 am
rate this thread
I recently installed APC (http://pecl.php.net/package/APC ) to see how
it works. Currently, I'm just using the opcode (file) caching and
already the response time is about 4x faster. No code changes were
required and it was as easy as just enabling APC in php.ini.
Now I'm interested in trying out the "user variable caching" to see if
I can get any more improvement in response time. I know that this
will require code changes and the use of the APC functions (like
apc_add(), apc_fetch(), etc.)
For example, I have a configuration file, which has about 400+ defined
constants for the web application I'm working on. I've read in the
APC documentation and elsewhere that define() is notoriously slow, so
my questions are:
1. Will storing these constants in APC help?
2. How would you store these constants? Just use apc_add()? Or use
apc_ load_ constants() (the APC docs say that this doesn't work as
well as expected?!)?
3. If I'm already using the opcode (file) caching, will it make any
difference store the 400+ defined constans in APC?
4. Any links to really good APC howtos or tutorials would be greatly
I found these presentations, which have some good pointers, but lack a
lot of details:
In one of the slides, it says Facebook stores/retrieves around 500,000
user objects/variables to and from their APC cache.
5. Anyone have any ideas why so many variables? I don't believe the
objects/variables pertain to individual users, because 500,000 would
be too few with over 200 million users. So what else could it be? I
find it hard to believe that configuration data and even all the
variables in the files would account for 500,000 variables/objects.
Thanks for any help and tips.
Re: Using APC to improve PHP performance?
Using class constants was faster (about 30x faster in my case) than
using define()s. The reason is define() is a runtime operation, while
class constants are resolved at compile-time.
By using class constants, you can simply use APC opcode (file)
caching. You don't even need the user cache, since the opcode
containing the constants is already cached.
You can access the class constants anywhere by using ::, like so:
The only question I have regarding this issue is if *accessing* and
using the constants is as fast or faster than the defined constants.
I would assume accessing either takes same amount of time, since they
all exist in some symbol table.