不要附加功能/方法定义增加程序的内存占用? [英] Do additional function/method definitions increase a program's memory footprint?

查看:204
本文介绍了不要附加功能/方法定义增加程序的内存占用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中,没有定义不习惯于导致更大的内存占用或附加的方法或函数慢执行速度?

基本上,我有一个类,其中没有所需要的正常使用类的几个实用的调试方法。将它使在内存占用或速度方面的差异这些定义是否保持如果绝不会使用他们?例如:

  MyClass类
{
    上市:        //这个东西类的用户将使用
        INT DoSomething的(){...}        //只用来做的东西我肯定写正确类
        布尔isClassValid(){...}
};...MyClass的ClassInstance的();
myClass.doSomething();


解决方案

请注意,大多数操作系统并不总是把所有的code在内存中。由于code为常数操作系统始终可以从需求文件加载它喜欢它会从交换加载动态数据。不过,这并不意味着未使用code是从来没有因为在操作系统加载它​​不是单独的方法而是通过网页加载。换句话说,它是predict,你的code段的部分在内存中实际上最终,除非你有你的操作系统非常深刻的认识和你的code段的结构很辛苦。可以肯定的说的唯一的事情是,它是完全有可能为你的code消耗比其实际大小的物理内存更少。

对于执行速度,答案是否定的,我认为。它可能会增加应用程序加载速度,但执行code时,无人问津这是多么大,它有速度绝对没有影响。也就是说,除非你是靠近你的内存限制,操作系统开始换了很多,一切都变得很慢。

由于其他人已经提到,编译器可以优化您的code OUT。但是,这也是东西,你可以用#ifdefs来你的调试方法做自己,通常被建议这样做。

In C++, does defining additional methods or functions that don't get used result in a larger memory footprint or slower execution speed?

Basically, I have several utility debugging methods in a class, none of which are required for the normal use of the class. Would it make a difference in terms of memory footprint or speed whether or not these definitions remain if they're never used? For example:

class myClass
{
    public:

        //Something the user of this class would use
        int doSomething() {...}

        //Something used solely to make sure I wrote the class properly
        bool isClassValid() {...}
};

...

myClass classInstance();
myClass.doSomething();

解决方案

Note that most OSes don't always keep all the code in the memory. Since the code is constant data the OS can always load it from file on demand like it would load dynamic data from swap. But it doesn't mean that unused code is never loaded since the OS loads it not by separate methods but by pages. In other words, it is very hard to predict which parts of your code segment actually end up in the memory unless you have very deep knowledge of your OS and the structure of your code segment. The only thing that can be said for sure is that it is perfectly possibly for your code to consume less physical memory than its actual size.

As for the execution speed, the answer is no I think. It may increase application loading speed, but when the code is executed, nobody cares how large it is and it has absolutely no effect on speed. That is, unless you are near your memory limit and the OS starts to swap a lot and everything becomes very slow.

As the others have already mentioned, the compiler may optimize your code out. But it is also something you can do yourself by using #ifdefs for your debugging methods and it is usually recommended to do so.

这篇关于不要附加功能/方法定义增加程序的内存占用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆