Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

increase loop performance by assign count() to variable #7413

Merged
merged 3 commits into from
May 4, 2015

Conversation

samsonasik
Copy link
Contributor

test :

$data = range(1, 100000);

$time_start1 = microtime(true);
for ($i = 0; $i < count($data); $i++) {

}
$time_end1 = microtime(true);
$time1 = $time_end1 - $time_start1;

echo 'uses count() in iteration take time : ' . $time1;
echo "\r\n";

$time_start2 = microtime(true);
for ($i = 0, $count = count($data); $i < $count; $i++) {

}
$time_end2 = microtime(true);
$time2 = $time_end2 - $time_start2;

echo 'uses assignment of count() before iterated first take time ' . $time2;
echo "\r\n";

Result :

uses count() directly in iteration take time : 0.11619091033936
uses assignment of count() before iterated first take time 0.017376184463501

@samsonasik samsonasik changed the title loop performance by assign count() to variable increase loop performance by assign count() to variable Apr 4, 2015
@@ -135,7 +135,8 @@ protected function processAddColumns(PlatformInterface $adapterPlatform = null)
if ($insert) {
$j = isset($j) ? $j : 0;
$sql = substr_replace($sql, $insert, $insertStart[$j], 0);
for (; $j < count($insertStart); ++$j) {
$countInsertStart = count($insertStart);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Count is a noun here not a verb, so it should be $insertStartCount instead of $countInsertStart.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ;)

@weierophinney weierophinney added this to the 2.4.1 milestone May 4, 2015
weierophinney added a commit that referenced this pull request May 4, 2015
increase loop performance by assign count() to variable
weierophinney added a commit that referenced this pull request May 4, 2015
@weierophinney weierophinney merged commit 15aa722 into zendframework:master May 4, 2015
weierophinney added a commit that referenced this pull request May 4, 2015
@samsonasik samsonasik deleted the performance-loop branch May 4, 2015 17:46
weierophinney added a commit to zendframework/zend-ldap that referenced this pull request May 15, 2015
…erformance-loop

increase loop performance by assign count() to variable
weierophinney added a commit to zendframework/zend-ldap that referenced this pull request May 15, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants