Hello,OOC World![Chap1Sec1-2][DRAFT]
Hello,OOC World![Chap1sec3-X][DRAFT 0.1.0518]

OOC与CPP生成代码大小对比

pingf posted @ Sun, 15 May 2011 22:45:46 -1100 in C语言 with tags OOC CPP , 3304 readers

今天做了一个简单的对比测试,看一下gcc模拟oo与g++生成代码(strip无用符号后)大小的比较

下面附上测试demo,先来OOC的

 

/*
 * test_Animals.c
 *
 *  Created on: 2011-5-16
 *      Author: Jesse Meng [pingf0@gmail.com]
 */
#include "OOStd.h"

CLASS(Animal){
	char *name;
	STATIC(Animal)
	vFn talk;
};
int Animal_reload(Animal *THIS,char *name){
	THIS->name=ALLOC(strlen(name)+1);
	memcpy(THIS->name,name,strlen(name));
	return 0;
}
int Animal_unload(Animal *THIS,void *PARAM){
	FREE(THIS->name);
	return 0;
}
ASM(Animal,Animal_reload,Animal_unload,NULL,NULL)

CLASS_EX(Animal,Cat){
	STATIC_EX(Animal,Cat)
};
static void Meow(Animal *THIS){
	printf("Meow!My name is %s!\n",THIS->name);
}
int Cat_reloadSt(StAnimal *THIS,void *PARAM){
	THIS->talk=(void *)Meow;
	return 0;
}
ASM_EX(Animal,Cat,NULL,NULL,Cat_reloadSt,NULL)


CLASS_EX(Animal,Dog){
	STATIC_EX(Animal,Dog)
};
static void Woof(Animal *THIS){
	printf("Woof!My name is %s!\n",THIS->name);
}
int Dog_reloadSt(StAnimal *THIS,void *PARAM){
	THIS->talk=(void *)Woof;
	return 0;
}
ASM_EX(Animal,Dog,NULL,NULL,Dog_reloadSt,NULL)

int main(){

	Animal *a[]={
			///////////////////////////
			(void*)newDog("Jack"),
			(void *)newCat("Lily"),
			(void*)newDog("Mike"),
			(void *)newCat("Lucy"),

//       100个,这里懒得写了,因为测试时是cccv的 .....

	};

	int i=0;
	//StAnimal *f=ST((Dog*)(a[0]));
	StAnimal *f=ST((Dog*)(a[0]));
	for(i=0;i<100;++i){
		f->talk(a[i]);
	}

	for(i=0;i<100;++i){
		//delDog((Dog**)&(a[i]),NULL);
		delDog((Dog**)&(a[i]),NULL);
	}
	return 0;
}

下面来看下CPP的

 

#include <iostream>
#include <string>

using namespace std;

class Animal
{
        public:
        Animal(const string& name) : name(name) {}
        virtual void talk() = 0;
        const string name;
};

class Cat : public Animal
{
        public:
        Cat(const string& name) : Animal(name) {}
        virtual void talk() { cout<<"Meow!Myname is "+name<<endl; }
};

class Dog : public Animal
{
        public:
        Dog(const string& name) : Animal(name) {}
        virtual void talk() { cout<<"Woof!Myname is "+name<<endl; }
};
 
int main()
{
        Animal* animals[] =
        {
        		///////////////
        	new Dog("Jack"),
        	new Dog("Mike"),
                new Cat("Lily"),
                new Cat("Lucy"),
//     这里同样100个 .....

        };

        for(int i = 0; i < 100; i++){
            animals[i]->talk();
        }
        for(int i = 0; i < 100; i++){
        	delete animals[i];
        }
        return 0;
}

尽管我已经将OOC-GCC写的足够简单了,但还是比CPP写出来稍长一点(感觉还好一般20行左右)

不过编译出来的我还是比较满意的,

上面两段代码输出一致,风格一致,都是最基本的形式.

类设计为一个基类,两个近似的继承类,100个任意继承类的对象.

先生成100堆对象,执行100次各对象的基本方法,然后逐一销毁.

实际测试后cpp编译出来的体积为32246,通过OOC-GCC宏出来仅为21518(而这里面还包括内存分配日志,时间测试等函数的调用)

这个多少有点出乎我的意料,因为OOC-GCC通过宏引入了相当多的辅助函数,比如上面的实际用到的关于对象辅助生成的函数应该至少有24个,而且每个体积也不小的.

 

lucky patcher said:
Fri, 10 Nov 2017 20:30:33 -1100

If any page has quality posts like yours then I will search for information very quickly. Please donate again

素材火 said:
Sun, 26 Nov 2017 16:16:13 -1100

c/c++视频教程
http://www.sucaihuo.com/video/197-0-0

NCERT Sanskrit Quest said:
Wed, 28 Sep 2022 06:02:31 -1100

Generally the Sanskrit sample papers are prepared by the subject experts of Leading Educational Institutes for all 11th standard students with all important questions. In fact, NCERT Sanskrit Question Paper Class 11 the questions that are given in Sanskrit model papers are taken from the 11th Standard Sanskrit studying syllabus which is mentioned in NCERT Sanskrit sample papers 2023 class 11.Generally the Sanskrit sample papers are prepared by the subject experts of Leading Educational Institutes for all 11th standard students with all important questions. In fact.


Login *


loading captcha image...
(type the code from the image)
or Ctrl+Enter