设为首页 加入收藏

TOP

C++ Built-In Array 的语义(一)
2015-07-20 17:43:37 来源: 作者: 【 】 浏览:2
Tags:Built-In Array 语义
A 是有默认的构造函数(但没有destructor)的类
我们有一个简单的struct:
?
struct StackObject
?
{
?
? ? ? ?int _a;
?
? ? ? ?int _b;
?
?
?
? ? ? ?StackObject(): _a(0), _b(1)
?
? ? ? ?{
?
? ? ? ?}
?
?
?
};
?
和简单的测试函数:
?
void TestArraySemantics()
?
{
?
? ? ? ?StackObject sa[10]; //line 1:call vector constructor iterator
?
? ? ? ?sa[0]._a= 1;
?
? ? ? ?sa[9]._b = 10;
?
? ? ? ? ? ??
?
}
?
在VC++2010中运行时,我们看到line1调用了编译自生的函数。这是一个通用的“矩阵构造循环(vector constructor iterator)”:
?
它的大致实施如下:
?
void Vector_constructor_iterator(
?
int array_size,
?
int array_element_size,
?
void (*Ctr)(void *addr),
?
char *arrayStartAddress)
?
{
?
? ? ? for(int i = 0; i < array_size; ++i)
?
? ? ? {
?
? ? ? ? ? ? void *objAddr = arrayStartAddress + i * array_element_size;
?
? ? ? ? ? ? Ctr(objAddr);
?
? ? ? }
?
}
?
这是一个典型的 C 函数,它将 StackObject 的构造函数作为函数指针进行调用。
?
从这个函数来看,它使得A a[3]; 和A a1,a2, a3; 的语义发生了根本变化。我们不但要调用编译产生的函数,还要用指针间接地调用StackObject的构造函数。测试结果显示,用array比不用array的“构造”速度大约下降30%。考虑到array的应用价值,这个速度的下降是可以理解和接受的。
?
A 是有默认构造函数以及destructor的类
加了destructor后,A a[10] 的语义又有了新的变化。如果读了我的上篇关于异常处理的博客(http://www.cnblogs.com/ly8838/p/3961119. html )可知:编译必须保证所有创建的object“全部”被摧毁,所以它必须“记住”创建过程中的热点。
?
我们加另一个dummy 类,来测试destructor对array 的影响:
?
struct StackObject2
?
{
?
? ? ? ?int _a;
?
? ? ? ?int _b;
?
? ? ? ?StackObject2(): _a(0), _b(1)
?
? ? ? ?{
?
? ? ? ?}
?
? ? ? ?~StackObject2()
?
? ? ? ?{
?
? ? ? ? ? ? ?_a = _b = 0;
?
? ? ? ?}
?
};
?
我们的测试函数改为
?
void TestArraySemantics()
?
? ? ? ?{
?
? ? ? ? ? ? ?clock_t begin = clock();
?
? ? ? ? ? ? ?for (int i = 0; i < 100000; ++i)
?
? ? ? ? ? ? ?{ ? ? ? ? ??
?
? ? ? ? ? ? ? ? ? ? StackObject sa[3]; //line1:test without d’tr
?
? ? ? ? ? ? ? ? ? ? sa[0]._a= 1;
?
? ? ? ? ? ? ? ? ? ? sa[1]._b = 2;
?
? ? ? ? ? ? ? ? ? ? sa[2]._b = 3;
?
? ? ? ? ? ? ?}
?
? ? ? ? ? ? ?clock_t end = clock();
?
? ? ? ? ? ? ?auto spent = double(end - begin) / CLOCKS_PER_SEC;
?
? ? ? ? ? ? ?printf("spent for 'array' is %f\n", spent);
?
?
?
? ? ? ? ? ? ?begin = clock();
?
? ? ? ? ? ? ?for (int i = 0; i < 100000; ++i)
?
? ? ? ? ? ? ?{ ? ? ? ? ??
?
? ? ? ? ? ? ? ? ? ? StackObject2 sa[3]; //line2:test with d’tr
?
? ? ? ? ? ? ? ? ? ? sa[0]._a= 1;
?
? ? ? ? ? ? ? ? ? ? sa[1]._b = 2;
?
? ? ? ? ? ? ? ? ? ? sa[2]._b = 3;
?
? ? ? ? ? ? ?}
?
? ? ? ? ? ? end = clock();
?
? ? ? ? ? ? ?spent = double(end - begin) / CLOCKS_PER_SEC;
?
? ? ? ? ? ? ?printf("spent for 'array with dtro' is %f\n", spent);
?
?
?
? ? ? ? ? ? ?begin = clock();
?
? ? ? ? ? ? ?for (int i = 0; i < 100090; ++i)
?
? ? ? ? ? ? ?{ ? ? ? ? ??
?
? ? ? ? ? ? ? ? ? ? StackObject sa1, sa2, sa3; //line3:test without array
?
? ? ? ? ? ? ? ? ? ? sa1._a= 1;
?
? ? ? ? ? ? ? ? ? ? sa2._b = 2;
?
? ? ? ? ? ? ? ? ? ? sa3._b = 3;
?
? ? ? ? ? ? ?}
?
? ? ? ? ? ?end = clock();
?
? ? ? ? ? ?spent = double(end - begin) / CLOCKS_PER_SEC;
?
? ? ? ? ? ? ?printf("spent for 'none-array' is %f\n", spent);
?
? ? ? ? ? ??
?
? ? ? ?}
?
?
?
上面的line2试图创建带有destructor的StackObject2类的array. 在VC++,我们注意到这时编译产生另一名字稍稍不同的函数“eh vector constructor iterator”,然而察看生成的代码,发现它和vector constructor iterator大大不同了。它的伪码大致如此:?
?
? ? ? ?void Vector_constructor_iterator_with_dtor(
?
? ? ? ? ? ? ?int array_size,
?
? ? ? ? ? ? ?int array_element_size,
?
? ? ? ? ? ? ?void (*Ctr)(void *addr),
?
? ? ? ? ? ? ?void (*Dtr)(void *addr),
?
? ? ? ? ? ? ?cha
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇uva 12086 - Potentiometers (树.. 下一篇hdu 4731 Minimum palindrome(构..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++中智能指针的性能 (2025-12-25 03:49:29)
·如何用智能指针实现c (2025-12-25 03:49:27)
·如何在 C 语言中管理 (2025-12-25 03:20:14)
·C语言和内存管理有什 (2025-12-25 03:20:11)
·为什么C语言从不被淘 (2025-12-25 03:20:08)