Can defining a lot of constants cause performance or memory problems?
Asked Answered
P

5

15

I have a website which uses a lot of constants, that are defined like this, for example:

define('CONSTANT', 1)

I currently have a list of about 200 defines like this. This list is run every time a page loads. Is this going to affect my performance or memory use badly?

Pullover answered 15/8, 2011 at 6:11 Comment(1)
imrannazar.com/Memory-Usage-of-Constants-in-PHP - The memory usage for global constants is the same as with an array. (40KB for 200 entries)Nag
S
12

There are reasons to avoid a list of 200 constants on every page load, but performance and memory usage would not be among them. In general, the best way to answer these kinds of questions is to run benchmarks. Measure page load time and memory usage both with and without loading your 200 constants. Measure a few thousand times and look at the numbers. I suspect you'll find a negligible difference. Micro-optimization is usually a waste of time.

Studious answered 15/8, 2011 at 6:16 Comment(0)
N
5

I would imagine not. Are you having performance issues with your script? If not, then don't worry about it. If so, then what have you done to determine where the bottleneck lies?

If you need to know how long it's taking to include and parse the file with your defines in, then I'd suggest you time it.

$start = microtime (true);
include ('file_to_include.php');
echo (microtime (true) - $start);
Neral answered 15/8, 2011 at 6:16 Comment(0)
H
1

Why all of the defines? it will likely only slow you down if you have a lot of traffic. if you are putting them all to use i could see that slowing it down pretty good. what's the reason maybe there is a better way (i've never needed more than 5 constants)

Handset answered 15/8, 2011 at 6:15 Comment(4)
"i've never needed more than 5 constants" --- yes, and 640kb is enough for everybodyHisakohisbe
Wait, does that mean you use literals instead?Artemisa
@zerkms: Apparently Bill Gates never said that.Studious
@Asaph: I know that, but that quote (either real or fabricated) perfectly fits the discussion, imho ;-)Hisakohisbe
A
0

I don't think it will have more impact than reading the file that contains 200 constants, then again it will be optimized by your disk cache. At least it is better than reading 200 constants from database. If you do care about performance, install an op cache, therefore, it will not require any additional parsing every time. For memory it depends on the type of your constants but it wont be much.

Amalberga answered 15/8, 2011 at 6:21 Comment(0)
A
0

Do it, don't worry.

<?php
$myfile = fopen("benchmark.php", "w") or die("Unable to open file!");
$txt = '<?php
';
$total = 1000;
for ($i = 1; $i <= $total; $i++) {
    $txt .= 'define("const_' . $i . '","adfdsfdsf' . $i . 'adsfdf");
    ';
}
$txt .= 'define("foo","adfdsf");';
$txt .= 'echo foo;';
fwrite($myfile, $txt);
fclose($myfile);

results

A simple hello World 20,000 qps

$total = 1000; 19,000 qps

$total = 5000; 12,000 qps

Antakiya answered 23/3 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.