PHP for循环vs C for循环 [英] PHP for loop vs C for loop

查看:227
本文介绍了PHP for循环vs C for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在PHP中执行下面的命令是不好的(因为每次迭代都会调用 count($ array),这会严重影响脚本的执行速度): ($ i = 0; $ i


$ p $ < + $ i)
{
//代码在这里;

$ / code>

相反,应该计算循环外的条件:

 <?php 
$ a = count($ array);
$ b $($ i = 0; $ i <$ a; ++ $ i)
{
//代码在这里;
}

我是编译语言的新手,所以我得到一个问题:



同样的规则是否适用于像C和C ++这样的编译语言?

我想在C ++中迭代矢量。我应该避免这样的循环或它是罚款?

  for(int i = 0; i< vector.size() ; ++ i)
{
//代码
}



<如果这不是编译语言中的问题 - 是因为编译器负责处理并优化了可执行文件,或者有其他原因吗?

解决方案问题归结为编译器是否知道编译时的答案?

在给出的情况下,答案是no 。因为可以在循环内改变向量的大小,所以它应该每次都进行评估(但是它相当快,因为​​它一次不计算一个)。

代码如下:

  int myArray [20]; 
for(int ii = 0; ii< sizeof(myArray)/ sizeof(* myArray); ii ++){

事实上会在编译时评估 sizeof ,并且因为这个效率会稍微高一些。注意 - 你需要整个表达式,因为在这种情况下, sizeof(myArray)本身将会返回 80 code> int 是四个字节)。使用 sizeof(* myArray)的好处在于,如果您改变了对 myArray 类型的想法,的代码不会中断...

事实上,你在这里使用一个分区并不会减慢执行速度,因为它是在编译期间完成的(只有一次) 。

重新说明我在评论中所说的内容:在这个例子中,你提出的版本在评估每次迭代时向量的大小的时候是真的不是问题。除了最严格的循环之外,你几乎看不到所有的性能差异。不要陷入陷阱微型优化



以下是一个简单的时间示例:

  #include< iostream> 
#include< ctime>
#include< vector>

使用namespace std;

int main(void){
vector< int> testVector(200);
int ii,jj;
寄存器int ss;
time_t startT,endT;

//案例1:使用常量for循环条件
startT = clock();对于(ii = 0; ii <100000; ii ++){
,对于(jj = 0; jj <200; jj ++){
testVector [jj] = ii -jj;
}
}
endT = clock();
printf(using constant:elapsed time:%.2f ms \\\
,(endT - startT)* 1000.0 / CLOCKS_PER_SEC);

// case 2:using size():
startT = clock();对于(jj = 0; jj testVector [jj] = ii(ii),
为(ii = 0; ii <100000; ii ++) - jj;
}
}
endT = clock();
printf(using size:elapsed time:%.2f ms \\\
,(endT - startT)* 1000.0 / CLOCKS_PER_SEC);

// case 3:单个调用size():
startT = clock();
ss = testVector.size();对于(ij = 0; jj testVector [jj] = ii-jj;(bj = 0; ii <100000; ii ++)
}
}
endT = clock();
printf(size with out of loop:elapsed time:%.2f ms \\\
,(endT - startT)* 1000.0 / CLOCKS_PER_SEC);



$ b $ p
pre





$ b $ $ b

 使用常量:已用时间:162.47 ms 
使用大小:已用时间:277.02 ms
超过循环的大小:已用时间:241.01 ms

正如您所看到的那样 是有限的时间在每个循环中增加向量的大小;但是对于20,000,000个呼叫,每个呼叫约为5ms。这听起来是对的;正如你所看到的,当你的圈足够紧的时候是可以衡量的;但是在大多数真实代码实例中(在循环中可能会做的更多),它不太可能具有实际意义。正如你所看到的,只是把调用移到循环之外对于 size 有帮助 - 使用一个常量和一个变量是一个更大的区别。

It is known that to do following in PHP is bad idea (because count ($array) will be called on every iteration which can seriously slow down script execution):

<?php
for ( $i = 0; $i < count ($array); ++$i )
{
   // Code here;
}

Instead one should calculate condition outside of the loop:

<?php
$a = count ($array);

for ( $i = 0; $i < $a; ++$i )
{
   // Code here;
}

I am new to compiled languages so I got a question:

Does the same rule applies to compiled languages like C and C++ for example?

Let's say I want to iterate vector in C++. Should I avoid such for loop or it is fine?

for ( int i = 0; i < vector.size(); ++i )
{
    // Code here
}

If this is not an issue in compiled languages - is it because compiler takes care for this and optimizes executable file or there is another reason behind that?

解决方案

The question comes down to "does the compiler know the answer at compile time"?

In the case you gave, the answer is "no". Since it is possible to change the size of the vector inside the loop it should get evaluated every time (but it's pretty fast since it's not counting one at a time).

Code like this:

int myArray[20];
for(int ii = 0; ii < sizeof(myArray)/sizeof(*myArray); ii++) {

will in fact evaluate sizeof at compile time, and be very marginally more efficient because of this. Note - you need the whole expression since sizeof(myArray) by itself will return 80 in this case (where an int is four bytes). The advantage of using sizeof(*myArray) is that if you change your mind about the type of myArray, this line of code doesn't break…

And the fact that you are using a division here doesn't slow down execution, since it's all done during compile time (only once).

To re-iterate what I said in my comment: in this instance, the version you are proposing, whilst evaluating the size of the vector on each iteration, is really not a problem. It is extremely unlikely that you could see a performance difference in all but the tightest loops. Don't fall into the trap of micro-optimization

Here is a simple timing example:

#include <iostream>
#include <ctime>
#include <vector>

using namespace std;

int main(void) {
  vector<int> testVector(200);
  int ii, jj;
  register int ss;
  time_t startT, endT;

  // case 1: using a constant for loop condition
  startT = clock();
  for(ii = 0; ii < 100000; ii++) {
    for(jj = 0; jj < 200; jj++) {
      testVector[jj] = ii - jj;
    }
  }
  endT = clock();
  printf("using constant: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC);

  // case 2: using size():
  startT = clock();
  for(ii = 0; ii < 100000; ii++) {
    for(jj = 0; jj < testVector.size(); jj++) {
      testVector[jj] = ii - jj;
    }
  }
  endT = clock();
  printf("using size: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC);

  // case 3: single call to size():
  startT = clock();
  ss = testVector.size();
  for(ii = 0; ii < 100000; ii++) {
    for(jj = 0; jj < ss; jj++) {
      testVector[jj] = ii - jj;
    }
  }
  endT = clock();
  printf("with size out of loop: elapsed time: %.2f ms\n", (endT - startT) * 1000.0 / CLOCKS_PER_SEC);
}

This gives the following result:

using constant: elapsed time: 162.47 ms
using size: elapsed time: 277.02 ms
with size out of loop: elapsed time: 241.01 ms

As you can see, there is a finite amount of time associated with looking up the size of the vector in every loop; but it is about 100 ms for 20,000,000 calls, or 5 ns per call. That sounds about right; as you can see, when your loop is tight enough it is measurable; but it is unlikely to be of practical significance in most "real" code instances (where you are likely to do more in your loop). As you can also see, just moving the call to size out of the loop doest help much - using a constant vs a variable is a bigger difference.

这篇关于PHP for循环vs C for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆