Help - Search - Members - Calendar
Full Version: Constants vs Variables
Invision Power Services > Community Forums > Community Web Design and Coding
Mert
Is there any speed difference between constants and variables? I have a settings table for each application using my framework and right after execution starts, I define each setting as a constant as I won't make any changes on the data. Would it work faster with variables (if such a difference exists)?

Besides, I don't really like using constants as they don't look aesthetic laughing.gif However I'm lazy enough to not to change them to variables without any reason.

Bear in mind that I will have to transport those variables via my singleton class so take that into account while calculating speed.
Davy
I don't know about PHP, but I'd imagine it'd be faster. From what I know with other languages, constants are replaced before compile/processing whereas variables take up memory and are referenced from memory each time they're used.
Mert
Yeah, that's what I've thought but just wanted to be sure original.gif
bfarber
I wouldn't be so sure.

http://planetozh.com/blog/2006/06/php-vari...s-vs-constants/

In my own benchmarking I've noticed similar results. The call to define() itself, additionally, has always been slower than I would have expected for such a simple statement. Do your own benchmarking using both methods if you really care either way.
Mert
I've just tested and yes, variables seem to be faster than constants with a 1/3 difference. I'd expect constants to be faster than variables as they are defined only once and used throughout the script with the same value.
bfarber
Quite an oddity huh? wink.gif

I don't know the reasons myself. Just know what benchmarking tests I've run and seen show, and it sounds like you're seeing the same thing.

Oh, but to note, we're talking microseconds even in extreme cases. Still, I'd never use constants on the argument that they're faster.
Brendon Koz
Without looking at any code, I'd *guess* it has something to do with PHP's automatic type-conversion. It adds extra rules to constants to prevent any modifications, and then throw an error, whereas such type protection does not have to be done anywhere else.

One would think it could be updated to address that issue by now, but...as Brandon just said, we're talking microseconds, and it'd probably be a core change that would have resounding effects on the rest of the core code as well.

...again, I'm just talking out of my butt...but it's a possibility I suppose. biggrin.gif
Mert
QUOTE (bfarber @ Aug 20 2008, 08:42 PM) *
Still, I'd never use constants on the argument that they're faster.


Yeah, I don't think I'll use it that often anymore. I was using it because of my laziness what did was fetching all settings and defining each one as a constant inside the loop (which is easy and global) instead of putting each value inside an array which is an object of my singleton class and calling where I need. I applied the first one to not to write "$this->settings = Registry::instance()->Settings" for each constructor pinch.gif
Davy
Interesting. I'm thinking more along the lines of C programming, where constants that are #DEFINE'd are simply search/replaced before compile. Told you I didn't know about PHP wink.gif
Mert
@Davy: But it was the logical one, right? tongue.gif

Found this article, maybe helps someone out there:

QUOTE (http://www.joomlaperformance.com/articles/performance/52_php_programming_tips_43_14.html)


Variable Operations


  • Operating on an initialized variable is 376% faster than operating on an unitialized variable.
  • Constants are 146% slower than variables
  • Local variables are 9.9% faster than global variables

String Functions

  • 'String' is 0.26% faster than "String"
  • "String" is 4% faster than HEREDOC syntax
  • "String\n" is 108% faster than 'String'."\n"
  • 'String'.$var is 28% faster than "String$var"
  • 'string '.$var.' string' is 55% faster than sprintf('string %s string', $var)
  • "\n" is 70% faster than chr(10)
  • strnatcmp() is 4.95% faster than strcmp()
  • strcasecmp() is 45% faster than preg_match()
  • strcasecmp() is 6.6% faster than strtoupper($string) == "STRING"
  • strcasecmp() is 13% faster than strnatcasecmp()
  • strtr($string, $string1, $string2) is 10% faster than str_replace()
  • str_replace() is 161% faster than strtr($string, $array)
  • stristr() is 10% faster than stripos()
  • strpos() is 9.7% faster than strstr()
  • isset($str{5}) is 176% faster than strlen($str) > 5
  • str_replace($str, $str, $str) twice is 17% faster than str_replace(array, array, string)
  • list() = explode() is 13% faster than substr($str, strpos($str))

Numeric Functions

  • ++$int is 10% faster than $int++
  • (float) is 48% faster than settype($var, 'float')

Array Functions

  • list() = $array; is 3.4% faster than assigning each variable
  • in_array() is 6% faster than array_search
  • isset($array[$key]) is 230% faster than array_key_exists()
  • !empty($array) is 66% faster than count($array)

Output Functions


  • echo is 5% faster than print()
  • echo ' '.' ' is 0.44% faster than echo ' ',' '

Functions And Methods

Function And Method Calling


  • call_user_func() is 54% slower than just calling the function
  • call_user_func() is 59% slower than calling a static method
  • call_user_func() is 65% slower than calling an object method
  • function() is 119% faster than static::method()
  • $this->method() is 116% faster than static::method()
  • declared static::method() is 93% faster than static::method()

Functions General

  • Pass by reference is 3% faster than Return by reference
  • No reference is 1.7% faster than Return by reference


Storage

File System

  • Scandir() is 4% faster than opendir(), readdir(), closedir()
  • file_get_contents() is 52% faster than fopen(), fread(), fclose()
  • file_get_contents() is 39% faster than implode("\n", file())

Cache Functions

  • xcache_set() is 1,645% faster than file_put_contents()
  • xcache_set() is 646% faster than memcache->set()
  • xcache_get() is 1,312% faster than memcache->get()

MISC Functions

General Functions

  • if elseif else is 0.78 % faster than switch
  • @Error supression is 235% slower than without
  • $_SERVER['REQUEST_TIME'] is 59% faster than time()
  • min(array) is 16% faster than min(int, int)
  • require_once() is 24% faster than include()
  • require_once() is just as fast as include_once()
  • include(relative path) is 37% faster than include(full path)

Regular Expressions

  • str_replace() is 40% faster than preg_replace()
  • ereg('regex') is 17% faster than preg_match('/regex/')
  • preg_match('/regex/i') is 68% faster than eregi('regex')
Jaggi
QUOTE (bfarber @ Aug 20 2008, 02:28 PM) *
I wouldn't be so sure.

http://planetozh.com/blog/2006/06/php-vari...s-vs-constants/

In my own benchmarking I've noticed similar results. The call to define() itself, additionally, has always been slower than I would have expected for such a simple statement. Do your own benchmarking using both methods if you really care either way.


that said using class const (php5) is ALOT faster than normal constants because you don't call the define() function. So this is definitely a grey area,.


QUOTE (Mert @ Aug 20 2008, 11:06 PM) *
Yeah, I don't think I'll use it that often anymore. I was using it because of my laziness what did was fetching all settings and defining each one as a constant inside the loop (which is easy and global) instead of putting each value inside an array which is an object of my singleton class and calling where I need. I applied the first one to not to write "$this->settings = Registry::instance()->Settings" for each constructor pinch.gif


I always use a singleton for my settings as it provides the userbility with the diversity that i require and a extra line of code doesn't bother me much.
Brendon Koz
Wow, thanks for that post from Joomla, Mert!
bfarber
QUOTE (Jaggi @ Aug 22 2008, 09:50 AM) *
that said using class const (php5) is ALOT faster than normal constants because you don't call the define() function. So this is definitely a grey area,.


Yes, class constants are much faster than regular constants - they're supposed to be *extremely* fast overall, but haven't profiled them.
Mert
QUOTE (Jaggi @ Aug 22 2008, 04:50 PM) *
I always use a singleton for my settings as it provides the userbility with the diversity that i require and a extra line of code doesn't bother me much.


Neither me but I have put that as a placeholder that's going to be changed sometime later. I've loaded all settings to an array which is an instance of my singleton class.

QUOTE (Brendon Koz @ Aug 22 2008, 07:00 PM) *
Wow, thanks for that post from Joomla, Mert!


You're welcome original.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2008 Invision Power Services, Inc.