![]() |
![]() |
![]()
Post
#1
|
|
![]() Grupa: Zarząd Postów: 2 277 Pomógł: 6 Dołączył: 27.12.2002 Skąd: Wołów/Wrocław ![]() |
Zazwyczaj mówi się o tym, by przy przekazywaniu dużych paramatrów do funckji używać referencji. Też wydawało mi sie to rozsądne, mimo ostrzeżeń z tym zwiazanych zawartych w manualu.
Jednak ostatnio trafiłem na artykuł http://phplens.com/lens/php-book/optimizin...bugging-php.php Zobaczcie, do czego tam doszli. Cytat Do References Speed Your Code?
References do not provide any performance benefits for non-object variables. For example, consider the following code: [php:1:9bf200aba9]<?php function TestRef(&$a) { $b = $a; $c = $a; } $one = 1; ProcessArrayRef($one); ?>[/php:1:9bf200aba9] And the same code without references: [php:1:9bf200aba9]<?php function TestNoRef($a) { $b = $a; $c = $a; } $one = 1; ProcessArrayNoRef($one); ?>[/php:1:9bf200aba9] php does not actually create duplicate variables when "pass by value" is used, but uses high speed reference counting internally. So in TestRef(), $b and $c take longer to set because the references have to be tracked, while in TestNoRef(), $b and $c just point to the original value of $a, and the reference counter is incremented. So TestNoRef() will execute faster than TestRef(). In contrast, functions that accept object parameters have a performance advantage when references are used. This is because objects do not use reference counting, so multiple copies of an object are created if "pass by value" is used. So the following code: [php:1:9bf200aba9]<?php function ObjRef(&$o) { $a =$o->name; } ?>[/php:1:9bf200aba9] is faster than: [php:1:9bf200aba9]<?php $function ObjRef($o) { $a = $o->name; } ?>[/php:1:9bf200aba9] Note: In php 5, all objects are passed by reference automatically, without the need of an explicit & in the parameter list. Czyli wynikałoby z tego, że zazwyczaj (nie mówię o obiektach) referencje nie są konieczne. Jak to ma się do Waszych doświadczeń? -------------------- "Niezależnie od tego, jakie masz osiągnięcia, ktoś Ci pomaga..."
|
|
|
![]() ![]() |
![]() |
Wersja Lo-Fi | Aktualny czas: 19.07.2025 - 05:56 |