0

有人向我推荐了一本书,名为:

Andrew Koenig 和 Barbara E. Moo Addison-Wesley 通过示例加速 C++ 实用编程,2000 ISBN 0-201-70353-X

这本书的基础是面向对象编程在内存方面非常浪费,并且大多数源代码不应该这样编写,而应该使用所有内联函数调用和过程编程。

我的意思是我知道大多数编程书籍的保质期和牛奶差不多,但是如果您编写客户端/服务器应用程序(数据库、服务器和所有)(不是设备驱动程序或视频游戏)真的值得无法维护的代码只是为了提高速度?

或者仅仅让应用程序在客户真正的旧机器上运行是否值得?或者能够在单个机器上运行更多服务器?

4

6 回答 6

8

哇,没有。

现代 C++ 编译器非常出色。大量内存使用更多是设计不佳或内存数据集大的症状。C++ 类所需的开销很小,现在真的不是问题。

面向对象编程是一种编写组件的方式,使得它们可以对与单个概念相关的动作进行逻辑分组(即,“汽车”的所有动作或“猫”的所有动作)。这并不是说它不能被滥用来编写意大利面条对象,但正如他们所说,您可以用任何语言编写 COBOL。

再举一个例子,现在用 C++ 和对象为嵌入式软件平台编写是很有可能并被接受的。轻微的速度下降和内存使用量增加(如果有的话)通过提高可维护性和代码可用性得到了千倍的回报。

于 2008-10-17T15:35:39.213 回答
7

I haven't read the book, but I have trouble believe that they wrote a book whose "basis ...is that Object Oriented Programming is highly wasteful memory-wise" (Full disclosure: Andy & Barbara are friends of mine).

Andy would never say the OOP is wasteful of memory. He WOULD say that a particular algorithm or technique is wasteful, and might recommend a less OO approach in some cases, but, he would be the first to argue that as a general rule OO designs are no more or less wasteful that any other style of programming.

The argument that OO designs are wasteful largely came from the fact that the EXEs of C++ "hello world" programs tend to be larger that the EXEs of C "hello world" programs. This is mostly because iostreams is larger the printf (but then, iostreams does more).

于 2008-10-17T15:54:56.477 回答
2

C++ and OOP are not inefficient per se, but it I have seen many C++ programs perform an operation in a less efficient manner than the equivalent C program. The biggest culprit is often due to lots of small memory allocations occurring due to newing individual objects rather than mallocing a whole bunch of them at once. Similarly, polymorphism and virtual functions are great, but they do incur an overhead that some C++ programmers are not aware of. Piecewise construction of objects can also be a lot slower than one dirty great memset of the aforementioned malloced array of structs.

My guess is that for most applications, on modern computers, this is not really an issue. Given that C++ also includes all of C as a subset, there is also nothing to stop you mixing and matching paradigms as situations demand. Newer heap handlers are also way better than the early MS efforts, and are a bg help.

于 2008-10-17T16:19:17.473 回答
2

The basis of this book is that Object Oriented Programming is highly wasteful memory-wise, and that most source-code should not be written this way, rather that you should use all inline function calls and procedural programming.

I would say this is a somewhat reductive summary of the book.

In short, to answer the title question, I would continue to recommend both the book and the concepts included therein.

I suspect that the advice is more along the lines that someone shouldn't create a class just to implement an algorithm, which I would say remains good advice.

于 2008-10-17T16:24:38.150 回答
0

想想开发人员一个小时的成本。

想想一个小时的 CPU 时间成本。

话虽如此,编码面向对象的性能损失绝对可以忽略不计。特别是考虑到当你的程序计算时,它会计算一些东西——这可能更多地取决于所使用的算法的性质,而不是是否使用 OOP。

于 2008-10-17T15:49:01.467 回答
-1

Some of the answers are totally missing the point. OOP in C++ has many opportunities to be much faster than their C counterparts. I'll give the example from I think Effective C++ by Scott Meyers, which is that quicksort runs slower than C++ sort because the compiler is able to inline the function call easily in C++ whereas it is unable to do so in C due to quicksort being passed a function pointer.

Additionally, nothing about c++ makes it slow, unlike other languages, any slowness is purely library implementations or algorithm design.

于 2008-10-17T19:02:59.047 回答