Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1059204
  • 博文数量: 77
  • 博客积分: 821
  • 博客等级: 军士长
  • 技术积分: 1905
  • 用 户 组: 普通用户
  • 注册时间: 2011-10-23 16:17
个人简介

学校:上海交通大学软件工程 学历:硕士 行业:从事流媒体移动开发 QQ: 412595942 邮箱:yiikai1987910@gmail.com

文章分类

全部博文(77)

文章存档

2016年(4)

2015年(15)

2014年(16)

2013年(12)

2012年(21)

2011年(9)

分类: C/C++

2011-10-29 11:13:05

   先看两个源文件
1.
  1. #include <stdio.h>
  2.     #include <string>
  3.     using namespace std;
  4.     class person
  5.     {
  6.         public:
  7.             person()
  8.             {
  9.                 ;
  10.             }
  11.             ~person()
  12.             {
  13.                 ;
  14.             }
  15.             virtual void eat()
  16.             {
  17.                 printf("I am a person to eat\n");
  18.             }
  19.         private:
  20.             std::string name;
  21.             std::string address;
  22.     };
  23.     class student : public person
  24.     {
  25.         public:
  26.             student()
  27.             {
  28.                 ;
  29.             }
  30.             ~student()
  31.             {
  32.                 ;
  33.             }
  34.             virtual void eat()
  35.             {
  36.                 printf("I am a student to eat\n");
  37.             }
  38.         private:
  39.             std::string schoolname;
  40.             std::string schoolAddress;
  41.     };
  42.     void test(student x)
  43.     {
  44.         x.eat();
  45.     }
  46.     /*
  47.     void test(student& x)
  48.     {
  49.         x.eat();
  50.     }*/
  51.     int main()
  52.     {
  53.         student x1;
  54.         for(int i = 0 ;i<10;i++)
  55.         {
  56.             test(x1);
  57.         }
  58.         return 0;
  59.     }
2.
  1. #include <stdio.h>
  2.     #include <string>
  3.     using namespace std;
  4.     class person
  5.     {
  6.         public:
  7.             person()
  8.             {
  9.                 ;
  10.             }
  11.             ~person()
  12.             {
  13.                 ;
  14.             }
  15.             virtual void eat()
  16.             {
  17.                 printf("I am a person to eat\n");
  18.             }
  19.         private:
  20.             std::string name;
  21.             std::string address;
  22.     };
  23.     class student : public person
  24.     {
  25.         public:
  26.             student()
  27.             {
  28.                 ;
  29.             }
  30.             ~student()
  31.             {
  32.                 ;
  33.             }
  34.             virtual void eat()
  35.             {
  36.                 printf("I am a student to eat\n");
  37.             }
  38.         private:
  39.             std::string schoolname;
  40.             std::string schoolAddress;
  41.     };
  42. /*
  43.     void test(student x)
  44.     {
  45.         x.eat();
  46.     }*/
  47.  
  48.     void test(student& x)
  49.     {
  50.         x.eat();
  51.     }

  52.     int main()
  53.     {
  54.         student x1;
  55.         for(int i = 0 ;i<10;i++)
  56.         {
  57.             test(x1);
  58.         }
  59.         return 0;
  60.     }
现在对这两个函数分别运行,进行计时,最后我们会惊奇的发现一个有趣的现象:
第一个程序的运行结果:
nick@nick-VirtualBox:~/Public/C++testpoj$ time ./a.out
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat

real    0m0.023s
user    0m0.000s
sys    0m0.000s

第二个程序的运行结果:
nick@nick-VirtualBox:~/Public/C++testpoj$ time ./a.out
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat
I am a student to eat

real    0m0.003s
user    0m0.004s
sys    0m0.000s

大家有没有发现,real的时间参数第二段代码的运行速度明显要比第一段的要快,这是为什么呢?
接下来我们就探寻一下!!!!!!!!!!!!!!
   这两端程序唯一的不同点就在于:
  1. void test(student x)
  2.     {
  3.         x.eat();
  4.     }
  5.  
  6.     void test(student& x)
  7.     {
  8.         x.eat();
  9.     }
这两个函数传递的参数不同,第一段代码是值传递,第二段是传递的对象引用
那为什么用引用的程序运行速度会比值传递的程序运行的快那么多呢?
   我们先看下第一个程序,main函数中,通过值传递参数的方法,本质上,会对传送的对象进行一次拷贝,即调用拷贝构造函数,然后当函数执行完,便要对这个对象进行析构,那又需要调用一次析构函数,然后,这只是一个开始,应为student对象是继承person对象,所以,在构造student对象前还要调用父类的拷贝构造函数,哪当然也要调用一次析构函数。我们还注意到两个类中的成员变量,在创建时也要调用构造析构函数,这样算一算,当传递一次值参的时候,系统偷偷的已经执行了6次拷贝构造,析构操作。
   而第二个程序通过引用传递的参数,则没有复制参数这个操作,我想有点c++基础的朋友都能想明白这个道理这里我就不讲了。
   从这点看,果然值传递的效率比引用传递慢了许多了!!!!!!
然而引用还有比值传递更优越的地方,下次我们继续讨论这点
阅读(1511) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~