9

Does using namespaces make a site faster or slower? That's what I would like to know.

If it helps improving site performance, then i would like to start using it right now. But if it degrades it, then i don't want to use it at all - even a little performance makes big difference in my project.

Dos anyone have a benchmark on this? What are your views?

Mat
  • 202,337
  • 40
  • 393
  • 406
Basit
  • 16,316
  • 31
  • 93
  • 154
  • 1
    possibly dupe to http://stackoverflow.com/questions/2709375/php-5-3-namespaces-should-i-use-every-php-function-with-backslash – konsolenfreddy Mar 16 '11 at 21:00
  • 2
    Hmm... think you might want to read about what a [namespace](http://www.php.net/manual/en/language.namespaces.rationale.php) actually is/does. – John Parker Mar 16 '11 at 21:00
  • 1
    Namespaces help to organize your code better (preventing class naming collisions etc.) working in environments with many different frameworks and classes they don't really offer any speed increase. – Alex Bailey Mar 16 '11 at 21:02
  • 1
    Is this really that bad of a question that it warrants this many downvotes? – Mike B Mar 17 '11 at 00:26
  • 4
    "even a little performance makes big difference in my project." **No it doesn't**. Performance has almost no bearing on 99.999999% of web-based projects. If it was a serious concern of yours, you wouldn't be using PHP. Make it maintainable. The most efficient code of all time won't be half as fast as slow code with some simple caching applied. – user229044 Mar 17 '11 at 16:04
  • Consider the costs of time, and make sure you're optimizing for the right thing. The cost of processor time is orders of magnitude smaller than the cost of programmer time. – Andy Lester Mar 17 '11 at 21:05

2 Answers2

19

PHP Namespaces are largely if not totally irrelevant for the performance of your site. They aid you at writing well-encapsulated and structured code - that's why you should not be afraid to use them, at least if your project reaches a certain complexity.

If you're really worried about performance, you should profile first and check where your real bottlenecks lie.

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
17

Namespace don't exist PHP-internally even as structure. Function and class names which are defined in a namespace will just have an identifier with an extra ASCII character within:

[1] => "namespace\user_func",    // get_defined_functions()

When PHP looks up a function/class/constant name, it has to traverse the same hash table as for ordinary functions/classes/constants. And since it is a hash-table, there is no performance penalty.

There is a difference for parsing, certainly. But it's not measurable.

mario
  • 144,265
  • 20
  • 237
  • 291