PHP variable name and length of SQL table column name
I have this question with a newbie :)
Although
$lastInvoiceNumber
$lastInvNum
or
last_invoice_number
(int 10) last_inv_num
(int 10)
Save some time to write. Do they have any
performance benefits (even the smallest) ?
Long versus short?
Is there a chance that php and MySQL are more important to consume less memory if the query had a shorter table column name?
For example, if I need to get 500 rows per query, I guess the query will run 500 times and run
last_invoice_number
500 times
vs running last_inv_num
might save memory or do something a little faster.
Thank.
source to share
No, there is no discernible performance difference at all, and you will get a huge improvement in readability with descriptive variable names. Internally, these variables are called memory addresses (simply put), not their ASCII / Unicode names. The impact it can have on performance, in virtually any language, is so infinitely small that it has never been noticed.
Edit:
I added a test. This shows that there is no difference between using a single letter as a variable name and using a 17 character variable name. A single letter might be a little slower. However, I do notice a slight consistent increase in time when using a 90 character variable name, but again, the difference is too small to ever be noticed for practical purposes. Here's the checkout and output:
<?php
# To prevent any startup-costs from skewing results of the first test.
$start = microtime(true);
for ($i = 0; $i<1000; $i++)
{
$noop = null;
}
$end = microtime(true);
# Let benchmark!
$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
$thisIsAReallyLongAndReallyDescriptiveVariableNameInFactItIsJustWayTooLongHonestlyWtf = mt_rand(0, 1000);
}
$end = microtime(true);
printf("Using a long name took %f seconds.\n", ($end - $start));
$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
$thisIsABitTooLong = mt_rand(0, 1000);
}
$end = microtime(true);
printf("Using a medium name took %f seconds.\n", ($end - $start));
$start = microtime(true);
for ($i = 0; $i<1000000; $i++)
{
$t = mt_rand(0, 1000);
}
$end = microtime(true);
printf("Using a short name took %f seconds.\n", ($end - $start));
Output:
$ php so-test.php
Using a long name took 0.148200 seconds.
Using a medium name took 0.142286 seconds.
Using a short name took 0.145952 seconds.
The same should be true for MySQL; I'd almost guarantee it, but it's not easy to compare. With MySQL, you will have a lot more network and I / O overhead than anything associated with naming characters in your code. As with PHP, internally, column names are not just strings that are repeated; data is stored in memory efficient formats.
source to share