嵌套循环谜题

以下哪两个代码段更快?假设编译器没有进行优化。

null

C++

/* FIRST */
for (i = 0; i < 10; i++)
for (j = 0; j < 100; j++)
//do something
// This code is contributed by Shubham Singh


C

/* FIRST */
for (i=0;i<10;i++)
for (j=0;j<100;j++)
//do something


蟒蛇3

# FIRST
for i in range ( 10 ):
for j in range ( 100 ):
#do something
# This code is contributed by shivani


C++

/* SECOND */
for (i=0;i<100;i++)
for (j=0;j<10;j++)
//do something
//This code is contributed by Shubham Singh


C

/* SECOND */
for (i=0;i<100;i++)
for (j=0;j<10;j++)
//do something


蟒蛇3

# SECOND
for i in range ( 100 ):
for j in range ( 10 ):
# Do something
# This code is contributed by shivani


两个代码段都提供相同的功能,两个for循环中的代码在两个代码段中执行的次数相同。 如果我们仔细观察,就会发现第二个比第一个做更多的操作。它执行for循环的所有三个部分(赋值、比较和增量)的次数比第一个循环的相应部分多:

  1. 第二个执行赋值操作(j=0或i=0)101次,而第一个只执行11次。
  2. 第二个进行101+1100比较(i<100或j<10),而第一个进行11+1010比较(i<10或j<100)。
  3. 第二个执行1100个增量操作(i++或j++),而第一个执行1010个增量操作。

下面的代码统计第一次和第二次执行的增量操作的数量,并打印计数。

C++

// C++ program to count number of increment
// operations in FIRST and SECOND
#include<iostream>
using namespace std;
int main()
{
int c1 = 0, c2 = 0;
/* FIRST */
for ( int i=0;i<10;i++,c1++)
for ( int j=0;j<100;j++, c1++);
/* SECOND */
for ( int i=0; i<100; i++, c2++)
for ( int j=0; j<10; j++, c2++);
cout << " Count in FIRST = " <<c1 << endl;
cout << " Count in SECOND  = " <<c2 << endl;
getchar ();
return 0;
}


JAVA

// Java program to count number of increment
// operations in FIRST and SECOND
import java.io.*;
class GFG {
public static void main (String[] args) {
int c1 = 0 , c2 = 0 ;
for ( int i= 0 ; i< 10 ;i++, c1++){
for ( int j= 0 ;j< 100 ;j++, c1++){}
}
for ( int i= 0 ;i< 100 ;i++, c2++){
for ( int j= 0 ;j< 10 ;j++, c2++){}
}
System.out.println( "Count in First = " +c1);
System.out.println( "Count in SECOND = " +c2);
}
}


蟒蛇3

# Python program to count number of increment
# operations in FIRST and SECOND
c1 = 0
c2 = 0
# FIRST
for i in range ( 10 ):
for j in range ( 100 ):
c1 + = 1
c1 + = 1
# SECOND
for i in range ( 100 ):
for j in range ( 10 ):
c2 + = 1
c2 + = 1
print ( "Count in FIRST = " ,c1)
print ( "Count in SECOND  = " ,c2)
# This code is contributed by shivanisinghss2110


C#

// C# program to count number of increment
// operations in FIRST and SECOND
using System;
class GFG{
public static void Main (String[] args)
{
int c1 = 0, c2 = 0;
for ( int i = 0; i < 10; i++, c1++)
{
for ( int j = 0; j < 100;j++, c1++){}
}
for ( int i = 0; i < 100; i++, c2++)
{
for ( int j = 0; j < 10; j++, c2++){}
}
Console.WriteLine( "Count in First = " + c1);
Console.WriteLine( "Count in SECOND = " + c2);
}
}
// This code is contributed by shivanisinghss2110


Javascript

<script>
// JavaScript program to count number of increment
// operations in FIRST and SECOND
let c1 = 0, c2 = 0;
for (let i=0; i<10;i++, c1++){
for (let j=0;j<100;j++, c1++){}
}
for (let i=0;i<100;i++, c2++){
for (let j=0;j<10;j++, c2++){}
}
document.write( "Count in First = " +c1 + "<br>" );
document.write( "Count in SECOND = " +c2);
// this code is contributed by shivanisinghss2110
</script>


输出

 Count in FIRST = 1010 Count in SECOND  = 1100

下面的代码统计第一个和第二个执行的比较操作的数量

C++

//program to count the number of comparison
//operations executed by FIRST and SECOND */
#include<iostream>
using namespace std;
int main()
{
int c1 = 0, c2 = 0;
/* FIRST */
for ( int i=0; ++c1&&i<10; i++)
for ( int j=0; ++c1&&j<100;j++);
/* SECOND */
for ( int i=0; ++c2&&i<100; i++)
for ( int j=0; ++c2&&j<10; j++);
cout << " Count for FIRST  " <<c1 << endl;
cout << " Count for SECOND  " <<c2 << endl;
getchar ();
return 0;
}


JAVA

/*package whatever //do not write package name here */
import java.io.*;
class GFG {
public static void main (String[] args) {
int c1 = 0 , c2 = 0 ;
for ( int i= 0 ; i<++c1 && i< 10 ;i++) {
for ( int j= 0 ;j<++c1 &&j< 100 ;j++) {
}
}
for ( int i= 0 ;i<++c2 && i< 100 ;i++) {
for ( int j= 0 ;j<++c2 &&j< 10 ;j++) {
}
}
System.out.println( "Count in FIRST = " +c1);
System.out.println( "Count in SECOND  = " +c2);
}
}


蟒蛇3

#program to count the number of comparison
#operations executed by FIRST and SECOND */
# FIRST
c1 = 1
c2 = 1
i = 0
while (i < c1 and i < 10 ):
j = - 1
c1 + = 1
while (j < c1 and j < 100 ):
c1 + = 1
j + = 1
i + = 1
# SECOND
i = 0
while (i < c2 and i < 100 ):
j = - 1
c2 + = 1
while (j < c2 and j < 10 ):
c2 + = 1
j + = 1
i + = 1
print ( " Count fot FIRST  " , c1)
print ( " Count fot SECOND  " , c2)
# This code is contributed by shivanisinghss2110


C#

/*package whatever //do not write package name here */
using System;
class GFG {
public static void Main (String[] args) {
int c1 = 0, c2 = 0;
for ( int i = 0; i < ++c1 && i < 10; i++)
{
for ( int j = 0; j < ++c1 && j < 100; j++)
{
}
}
for ( int i = 0; i < ++c2 && i < 100; i++) {
for ( int j = 0; j < ++c2 && j < 10; j++) {
}
}
Console.WriteLine( "Count in FIRST = " +c1);
Console.WriteLine( "Count in SECOND  = " +c2);
}
}
// This code is contributed by shivanisinghss2110


Javascript

<script>
/*package whatever //do not write package name here */
//program to count the number of comparison
//operations executed by FIRST and SECOND */
let c1 = 0, c2 = 0;
for (let i=0; i<++c1 && i<10;i++) {
for (let j=0;j<++c1 &&j<100;j++) {
}
}
for (let i=0;i<++c2 && i<100;i++) {
for (let j=0;j<++c2 &&j<10;j++) {
}
}
document.write( "Count in FIRST = " +c1 + "<br>" );
document.write( "Count in SECOND  = " +c2);
// this code is contributed by shivanisinghss2110
</script>


输出

 Count for FIRST  1021 Count for SECOND  1201

幸亏 Dheeraj 感谢你提出解决方案。 如果您发现任何答案/代码不正确,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享