简体   繁体   English

操作员重载STL的性能损失是多少

[英]What is the performance penalty of operator overloading STL

I like STL a lot. 我非常喜欢STL。 It makes coding algorithms very convenient since it provides you will all the primitives like parition, find, binary_search, iterators, priority_queue etc. Plus you dont have to worry about memory leaks at all. 它使编码算法非常方便,因为它为您提供了所有原语,例如,分区,查找,binary_search,迭代器,priority_queue等。此外,您完全不必担心内存泄漏。

My only concern is the performance penalty of operator overloading that is necessary to get STL working. 我唯一关心的是要使STL工作所需的运算符重载性能损失。 For comparison, I think it relies that == provides the needed semantics. 为了进行比较,我认为这取决于==提供所需的语义。 We need to overload ==operator if we are adding our classes to a container. 如果要将类添加到容器中,则需要重载== operator。

How much efficiency am I losing for this convenience? 我为这种便利而损失了多少效率?

Another aside question regarding memory leaks: 关于内存泄漏的另一个问题:

  1. Can memory leak ever happen when using STL containers? 使用STL容器时是否会发生内存泄漏?
  2. Can a memory leak ever happen in Java? Java是否会发生内存泄漏?

When using stl algortithms on generic types, you have to supply the comparison logic in some way. 在泛型类型上使用stl算法时,必须以某种方式提供比较逻辑。 Operator overloading has no performance penalty over any other function and may (like any other function) be inlined to remove any function call overhead. 运算符重载不会对任何其他函数造成性能损失,并且可以像其他任何函数一样内联以消除任何函数调用开销。

Many standard containers and algorithms also use std::less and hence by default < rather than == . 许多标准容器和算法也使用std::less ,因此默认情况下<而不是==

The standard containers don't themselves leak, but you can use them to hold objects (such as pointers) which don't necessarily clean up memory that they 'own'. 标准容器本身不会泄漏,但是您可以使用它们来容纳不一定清除其“拥有”内存的对象(例如指针)。

It's difficult to leak memory in java, but that doesn't mean you can't get into trouble by failing to have good object ownership semantics and it doesn't mean that you can't use up all the available memory and crash. 很难在Java中泄漏内存,但这并不意味着您不会因缺乏良好的对象所有权语义而陷入麻烦,也不意味着您无法耗尽所有可用的内存并导致崩溃。

I'll leave the C++ answers to the previous posters, but 100% yes you can memory leak in Java. 我将把C ++答案留给以前的海报,但100%是的,您可能会在Java中发生内存泄漏。 It tends to be really difficult to find too unless you have some good memory profiling tools to look at. 除非您有一些不错的内存分析工具可以查看,否则也很难找到它。 In general, memory leaks in automated garbage collection languages (eg Java, Python, etc.) occur when you repeatedly instantiate objects, but either A. don't clean them up when you're done with them (eg calling "close" on a database connection) or B. continue to persist other objects that point to them (eg Hashtables) so that the automated garbage collector can never garbage collect them. 通常,当您重复实例化对象时,会发生自动垃圾回收语言(例如Java,Python等)中的内存泄漏,但是A.在完成对对象的处理后(例如在调用“ close”时不清除它们)数据库连接)或B.继续保留指向它们的其他对象(例如Hashtables),以便自动垃圾收集器永远无法对其进行垃圾收集。

If your application is in what you think should be a stable state and you get one of these: 如果您的应用程序处于您认为应该处于稳定状态的状态,那么您将获得以下之一:

http://java.sun.com/javase/6/docs/api/java/lang/OutOfMemoryError.html http://java.sun.com/javase/6/docs/api/java/lang/OutOfMemoryError.html

You're in for some fun debuggin ;) 您正在进行一些有趣的调试;)

Since operator overloading simply results in a function call, and you would have to write a function to do the work anyways, the overhead is zero. 由于运算符重载只会导致函数调用,并且您必须编写一个函数来完成工作,因此开销为零。 Operator overloading is just a convenience so you can do things like x == y instead of x.equals(y) or x < y instead of x.compaterTo(y). 运算符重载只是一种便利,因此您可以执行x == y代替x.equals(y)或x <y代替x.compaterTo(y)之类的操作。 The compiler essentially generates something like: x.==(y) or x.<(y) (that won't compile, but you get the idea). 编译器本质上会生成类似x。==(y)或x。<(y)的东西(虽然不会编译,但是您可以理解)。

The "penalty" is actually a bonus. “罚款”实际上是一种奖金。

Let's take the most typical of algorithms, sorting. 让我们以最典型的算法进行排序。 C does not have operator overloading. C没有运算符重载。 As a result, qsort takes a function pointer. 结果, qsort了一个函数指针。 Each comparison uses an indirect function call (at runtime). 每个比较都使用一个间接函数调用(在运行时)。 C++ does have operator overloading. C ++确实有运算符重载。 For std::sort , each comparison is a direct call (fixed by linker) or inlined (by compiler). 对于std::sort ,每个比较都是直接调用(由链接器固定)或内联(由编译器)。 This is far mroe effective. 这是非常有效的。 It's not uncommon for std::sort to be 6 times faster than qsort . std::sortqsort快6倍并不罕见。

In general, operator overloading makes it a lot easier to express the "default" functions for a type, in a way that can be exploited by other code and compilers. 通常,运算符重载使类型的“默认”函数的表达变得容易得多,其方式可以被其他代码和编译器利用。 The alternatives are far less efficient. 替代方案的效率要低得多。 Either they rely on macros (which hurt programmers) or they rely on function pointers (which hurt optimizers and CPUs.) 他们要么依赖宏(对程序员不利),要么依赖函数指针(对优化程序和CPU不利)。

My only concern is the performance penalty of operator overloading that is necessary to get STL working. 我唯一关心的是要使STL工作所需的运算符重载性能损失。 For comparison, I think it relies that == provides the needed semantics. 为了进行比较,我认为这取决于==提供所需的语义。 We need to overload ==operator if we are adding our classes to a container. 如果要将类添加到容器中,则需要重载== operator。

It is nonexistent. 它不存在。 Your overloaded operator is implemented as a function call. 重载的运算符通过函数调用实现。 And if you didn't overload the operator, you'd need to define a function to use instead. 而且,如果您没有重载运算符,则需要定义一个函数来代替。 So the performance is exactly the same, just with a cleaner syntax. 因此,使用更简洁的语法,性能完全相同

What is the performance penalty of operator overloading STL 操作员重载STL的性能损失是多少

In addition to this you can always have your operators inline this will not even have the penalty of a function call. 除此之外,您始终可以让您的操作员内联,这甚至不会带来函数调用的损失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM