首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

新手想问几个小问题(100分全给了)

2012-12-30 
新手想问几个问题(100分全给了)问题1:while( --n 0) {} while (n-- 0) {} for ( --n) {} for (

新手想问几个问题(100分全给了)
问题1:

while( --n > 0) {} ;
while (n-- > 0) {} ;
for ( ; ; --n) {} ;
for ( ; ; n--) {} ;
      对于以上的四种循环,哪种效率更高呢?我看过有些人通过循环多次的运行时间来判断各语句的效率,那么在mingw或VC的测试代码该怎么写呢?(该用什么函数?)另外,有些人通过查看汇编程序来判断效率,但我没学过汇编,有没有简单办法能看懂汇编语句来够判断各语句的效率?

问题2:最近在研究指针,想问下C本身是否有一些定义好的指针。  比如说C里面的流就由stdin, stdout, stderr, 那么指针呢?

问题3:最近老师让我们多考虑容错性的问题,请问下各位有没有收藏有关容错性的文章,分享一下。


希望各位不吝赐教...在此先谢谢各位!
[解决办法]
问题1  看时间复杂度  while和for可以互换
就回一个啦  先睡觉啦
[解决办法]

#include "time.h"
#include "stdio.h"
#include "conio.h"
void main()
{
  time_t start,end;
  int i;
  start=time(NULL);
  for(i=0;i<30000;i++)
    printf("\1\1\1\1\1\1\1\1\1\1\n");
  end=time(NULL);
  printf("\1: The different is %6.3f\n",difftime(end,start));
  getch();
}

[解决办法]
问题1:
C/C++ code
?
1
2
3
4
while( --n > 0) {} ;
while (n-- > 0) {} ;
for ( ; ; --n) {} ;
for ( ; ; n--) {} ;
      对于以上的四种循环,哪种效率更高呢?我看过有些人通过循环多次的运行时间来判断各语句的效率,那么在mingw或VC的测试代码该怎么写呢?(该用什么函数?)另外,有些人通过查看汇编程序来判断效率,但我没学过汇编,有没有简单办法能看懂汇编语句来够判断各语句的效率?


vc下测试性能:
QueryPerformanceFrequency
。。。
QueryPerformanceCounter
while(){
}
QueryPerformanceCounter
获得的值相减后,看时间差值


问题2:最近在研究指针,想问下C本身是否有一些定义好的指针。  比如说C里面的流就由stdin, stdout, stderr, 那么指针呢?
C里面的stdin, stdout, stderr是文件系统的文件句柄(C++的不清楚,好像可以用*stdout(?),那就是把*这个操作重载了,但与C的本质是一样的),与指针没有关系。
C中没有预定义好的指针

问题3:最近老师让我们多考虑容错性的问题,请问下各位有没有收藏有关容错性的文章,分享一下。
c语言的容错性 个人理解就是 尽可能的处理各种非法条件,具体问题具体对待。
[解决办法]
第一个问题:
写了下面几个函数,编译并反汇编:
int fun1()
{
    int n=100;
    int p;
    while(--n>0)
    {
       p = p*n;
    }
    return p;
}
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 55 08                mov    0x8(%ebp),%edx
   6:   83 ea 01                sub    $0x1,%edx
   9:   85 d2                   test   %edx,%edx
   b:   7e 0d                   jle    1a <fun1+0x1a>


   d:   8d 76 00                lea    0x0(%esi),%esi
  10:   0f af c2                imul   %edx,%eax
  13:   83 ea 01                sub    $0x1,%edx
  16:   85 d2                   test   %edx,%edx
  18:   7f f6                   jg     10 <fun1+0x10>
  1a:   5d                      pop    %ebp
  1b:   c3                      ret

int fun2(int n)
{       
int p;    
while(n-->0)    
{       
p = p*n;
}    
  return p;
}
00000000 <fun2>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 55 08                mov    0x8(%ebp),%edx
   6:   85 d2                   test   %edx,%edx
   8:   7e 11                   jle    1b <fun+0x1b>
   a:   83 ea 01                sub    $0x1,%edx
   d:   8d 76 00                lea    0x0(%esi),%esi
  10:   0f af c2                imul   %edx,%eax
  13:   83 ea 01                sub    $0x1,%edx
  16:   83 fa ff                cmp    $0xffffffff,%edx
  19:   75 f5                   jne    10 <fun2+0x10>
  1b:   5d                      pop    %ebp


  1c:   c3                      ret 
编译选项均采用:gcc -c -O2 fun1.c
从汇编代码看,while循环中只差一个指令test   %edx,%edx和cmp    $0xffffffff,%edx,所以效率应该是一样的。

int fun1(int n)
{
    int p;
    for(; n>0; --n)
    {
       p = p*n;
    }
    return p;
}

00000000 <fun1>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 55 08                mov    0x8(%ebp),%edx
   6:   85 d2                   test   %edx,%edx
   8:   7e 10                   jle    1a <fun1+0x1a>
   a:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
  10:   0f af c2                imul   %edx,%eax
  13:   83 ea 01                sub    $0x1,%edx
  16:   85 d2                   test   %edx,%edx
  18:   7f f6                   jg     10 <fun1+0x10>
  1a:   5d                      pop    %ebp
  1b:   c3                      ret 
  
int fun2(int n)
{
    int p;
    for(; n>0; n--)
    {
       p = p*n;
    }
    return p;
}

  00000000 <fun2>:
   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   8b 55 08                mov    0x8(%ebp),%edx


   6:   85 d2                   test   %edx,%edx
   8:   7e 10                   jle    1a <fun1+0x1a>
   a:   8d b6 00 00 00 00       lea    0x0(%esi),%esi
  10:   0f af c2                imul   %edx,%eax
  13:   83 ea 01                sub    $0x1,%edx
  16:   85 d2                   test   %edx,%edx
  18:   7f f6                   jg     10 <fun2+0x10>
  1a:   5d                      pop    %ebp
  1b:   c3                      ret 
可以看到for的汇编代码是一样的,所以没差别
  

热点排行