Use functions inside a loop declaration

What’s the best practice?

This :

for ($i = 0; $i < count($array); $i++) {
//stuff
}

Or, what I usually do :

$count = count($array);
for($i = 0; $i < $count; $i++) {
//stuff
}

Is it the same with the magic of compiler optimization?

1

Profile and see it yourself.

In general, the first sample will execute count() on every iteration. This may drastically reduce the performance.

But:

  • In some languages and/or some cases, compilers may optimize it. But I wouldn’t rely on it, since most of the time, optimizing such thing is risky.

  • The performance difference may be too minor to bother.

For example, in C#, using a for (var i = 0; i < collection.Count(); i++) on a lazy collection which hits a database may lead to a disaster. No compilers would optimize this, since they won’t know if you’re just reading a read-only database or if you’re reading it while at the same time modifying it as well. But in all cases, you shouldn’t use for in a first place in such context.

A rule of thumb:

  • Use a variable outside for if you know that the collection will not change when you’re in a loop,

  • Count the number of elements in a for if the collection size might change when you’re in a loop,

  • Ask yourself it other techniques, like simply iterating through a collection until it ends, are not more appropriate.

it depends on whether //stuff changes $array (i.e. the length of the array).

The first variant is more explicit since you can directly see that the length of the array is the limit of the loop, it could be slightly slower than the second, but in interpreted languages this should not be too much of a problem.

3

The particular optimization in question goes by a couple of names typically hoisting or loop invariant code motion. It is a fairly common optimization performed by compilers. If you are in a compiled language don’t worry about it, it is probably taken care of. If you are in an interpreted language then weather or not it happens is less certain.

Most languages allow you to initialize multiple variables during the declaration of a for statement. In all languages the condition component of a for statement is evaluated for each iteration of the loop. So the following for($i=0; $i < foo(); $i++) would result in the function foo() being called in each iteration. As long as foo() continues to return a value higher than $i the loop to continue.

This is true in C++, C#, Java and PHP.

Now, in the last example let’s assume the function foo() returns a constant value, but it’s computationally heavy to calculate it. Therefore, calling foo() for each iteration would slow the execution of the code.

There are two common practices to solve this problem.

$x = foo();
for($i = 0; $i < $x; $i++) {.....}

And

for($i = 0, $x = foo(); $i < $x; $i++) {....}

I prefer the second as it’s easier to read what $x does, and what it’s used for.

EDIT:

To answer the question, if compiler optimization takes place. The answer is No. All compilers that I’ve seen will continue to evaluate the condition statement of a for loop for each iteration. If that condition contains a function call such as count() then it will be executed each time.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật