简体   繁体   English

C ++标准库与凡人制作代码+我在哪里可以找到来源?

[英]C++ standard library vs mortal made code + where can I find the sources?

Two, maybe trivial questions: 两个,也许是微不足道的问题:

1. Why can not I beat the STD functions? 1.为什么我不能击败STD功能?

Really. 真。 I spent the last three days implementing something faster than std::sort, just for the sake of doing it. 我花了最后三天实现比std :: sort更快的东西,只是为了做到这一点。 It is supposed to be an introsort, and I suspect it uses the single pivot version quicksort inside. 它应该是一个introsort,我怀疑它使用单个pivot版本quicksort里面。 Epic fail. 史诗失败。 Mine was at least twice as slow. 我的速度至少慢了两倍。

In my utter bitterness I even copy-pasted other - top notch - programmers code. 在我的痛苦中,我甚至复制粘贴其他 - 顶级 - 程序员代码。 No avail. 徒劳无功。 I benchmarked my other algorithms too... My binary search, and upper_bound, lower_bound versions are so stripped down it couldn't really be made with less instructions. 我也对我的其他算法进行了基准测试...我的二进制搜索,以及upper_bound,lower_bound版本都被剥离了,实际上并没有用更少的指令。 Still, they are about twice as slow. 不过,它们的速度大约是其两倍。

I ask, why, why, why? 我问,为什么,为什么,为什么? And this leads me to my next question... 这引出了我的下一个问题......

2. Where can I find the source code of the STL library functions? 2.在哪里可以找到STL库函数的源代码?

Of course, I want to look at their sources! 当然,我想看看他们的消息来源! Is it even possible to write more efficient code than those, or am I at an abstraction level with my "simple" main.cpp where I can not reach optimisations utilized by the STL library? 是否有可能编写比这些更高效的代码,或者我是否处于抽象级别与我的“简单”main.cpp,我无法达到STL库使用的优化?

I mean for example... Let's take the maps... wich are simple associative containers. 我的意思是举例......让我们拿地图......这是简单的联想容器。 The documentation says it is implemented with a red-black tree. 文档说它是用红黑树实现的。 Now... would it worth it to try implement my own red-black tree, or they took this joy :-) away from me and I should just throw every data I get my hands on into the map container? 现在......尝试实现我自己的红黑树是值得的,或者他们带着这种喜悦:-)远离我,我应该把我得到的所有数据都扔到地图容器中?

I hope this does make sense. 我希望这确实有意义。 If not, please forgive me. 如果没有,请原谅我。


The short answer is "if it was possible to write faster code which did the same thing, then the standard library would have done it already". 简短的回答是“如果可以编写更快的代码来执行相同的操作,那么标准库就已经完成了”。

The standard library is designed by clever people, and the reason it was made part of C++ is that other clever people recognized it as being clever. 标准库是由聪明的人设计的,它成为C ++的一部分的原因是其他聪明的人认为它是聪明的。 And since then, 15 years have passed in which other clever people tried to take these specifications and write the absolutely most efficient code to implement it that they could. 从那以后,15年过去了, 其他聪明的人试图采用这些规范并编写绝对最有效的代码来实现它们。

That's a lot of cleverness you're trying to compete with. 这是你想要与之竞争的很多聪明。 ;) ;)

So there is no magic in the STL, they don't cheat, or use tricks unavailable to you. 所以STL中没有魔法,他们不会作弊,也不会使用你无法使用的技巧。 It is just very carefully designed to maximize performance. 它经过精心设计,可以最大限度地提高性能。

The thing about C++ is that it's not a fast language as such. 关于C ++的事情是,它不是一种快速的语言。 If you're not careful, it is easy to introduce all sorts of inefficiencies: virtual function calls, cache misses, excessive memory allocations, unnecessary copying of objects, all of this can cripple the performance of C++ code if you're not careful. 如果你不小心,很容易引入各种低效:虚函数调用,缓存未命中,过多的内存分配,不必要的对象复制,所有这些都会削弱C ++代码的性能,如果你不小心的话。

With care, you can write code that's about as efficient as the STL. 小心,您可以编写与STL一样高效的代码。 It's not that special. 它并不那么特别。 But in general, the only way you're going to get faster code is to change the requirements. 但总的来说,获得更快代码的唯一方法就是改变需求。 The standard library is required to be general, to work as well as possible across all use cases. 标准库必须是通用的,以便在所有用例中尽可能地工作。 If your requirement is more specific, it is sometimes possible to write specialized code that favors those specific cases. 如果您的要求更具体,有时可以编写有利于这些特定情况的专用代码。 But then the tradeoff is that the code either will not work, or will be inefficient, in other cases. 但是,在其他情况下,权衡是代码要么不起作用,要么效率低下。

A final point is that a key part of the reason why the STL is so clever, and why it was adopted into the standard, is that it it is pretty much zero-overhead. 最后一点是STL如此聪明的原因以及为什么它被采用到标准中的一个关键部分是,它几乎是零开销。 The standard libraries in many languages are "fast enough", but not as fast as hand-rolled code. 许多语言的标准库“足够快”,但不如手动代码快。 They have a sorting algorithm, but it's not quite as fast as if you wrote it yourself in-place. 他们有一个排序算法,但它并不像你自己就地编写它那么快。 It might use a few casts to and from a common "object" base class, or maybe use boxing on value types. 它可能会在常见的“对象”基类中使用一些强制转换,也可能在值类型上使用装箱。 The STL is designed so that pretty much everything can be inlined by the compiler, yielding code equivalent to if you'd hand-rolled it yourself. STL的设计使得几乎所有东西都可以被编译器内联,产生的代码相当于你自己手动编写的代码。 It uses templates to specialize for the type you're using, so there's no overhead of converting to a type understood by the container or algorithm. 它使用模板专门针对您正在使用的类型,因此转换为容器或算法所理解的类型没有任何开销。

That's why it's hard to compete with. 这就是为什么很难与之竞争的原因。 It is a ridiculously efficient libary, and it had to be. 这是一个非常有效的图书馆,它必须是。 With the mentality of your average C or C++ programmer, especially 10-15 years ago, no one would ever use a std::vector if it was 5% slower than a raw array. 根据普通C或C ++程序员的心态,特别是10 - 15年前, 没有人会使用std::vector如果它比原始数组慢5%。 No one would use iterators and std algorithms if they weren't as fast as just writing the loop yourself. 没有人会使用迭代器和std算法,如果它们没有自己编写循环那么快。

So the STL pioneered a lot of clever C++ tricks in order to become just as efficient as hand-rolled C code. 因此,STL开创了许多聪明的C ++技巧,以便与手动C代码一样高效。

They are probably optimized to a great extent. 它们可能在很大程度上得到优化。 Such implementations consider memory page faults, cache misses etc. 此类实现考虑了内存页面错误,缓存未命中等。

Getting the source of those implementations depends on the compiler they are shipped with. 获取这些实现的来源取决于它们随附的编译器。 I think most compilers (even Microsoft) will allow you to see them. 我想大多数编译器(甚至微软)都会允许你看到它们。

I think the most important things to know are the architecture you are compiling to and the operating system (if any) your program will be running on. 我认为最重要的事情是你要编译的架构和你的程序将运行的操作系统(如果有的话)。 Understanding these things will allow you to precisely target the hardware. 了解这些内容可以让您精确定位硬件。

There are also countless optimization techniques. 还有无数的优化技术。 This is a good summary. 是一个很好的总结。 Also, global optimization is whole science, so there are certainly many things to learn. 此外,全球优化是整个科学,因此肯定有许多东西需要学习。

There are some clever things on this site, too. 这个网站上也有一些聪明的东西 Sok sikert! Sok sikert!

Looking at the disassembled version of your code versus their code and comparing them may give you some insights into why their is faster than yours. 查看代码的反汇编版本与其代码并进行比较可能会让您了解为什么它们比您的更快。

It seems like a fool's errand to reimplement from scratch standard library functionality for the sake of making faster versions. 为了制作更快的版本,从头开始重新实现标准库功能似乎是一件愚蠢的事。 You'd be far better served trying to modify their version to achieve your goals, although even then you really need to understand the underlying platform to be able to judge the value of the changes that you're making. 尝试修改他们的版本以实现目标会更好,尽管即便如此,您仍然需要了解底层平台才能判断您所做的更改的价值。

I would guess that were you to post your sort routine that it would be torn apart in minutes and you would gain an understanding of why your version is so substantially slower than the standard library version. 我猜你要发布你的排序例程,它会在几分钟内被拆散,你会理解为什么你的版本比标准库版本慢得多。

Most IDEs have a command to open a named header file using the compiler's search paths. 大多数IDE都有一个命令,可以使用编译器的搜索路径打开命名头文件。 I use this pretty often and tend to keep the code for algorithm open. 我经常使用它,并倾向于保持algorithm的代码打开。

For me, the code you're looking for is in 对我来说,你正在寻找的代码是

/usr/include/c++/4.2.1/bits/stl_algo.h
/usr/include/c++/4.2.1/bits/stl_tree.h

Note that a lot of people have done their theses on sorting and tree-balancing (fields I would think are picked to the bone, I would not attempt research there), and many of them are probably more determined than you to make GCC's standard library faster. 请注意,很多人已经完成了关于排序和树平衡的论文(我认为这些领域已被选中,我不会尝试在那里进行研究),其中许多人可能比你更有决心制作GCC的标准库快点。

That said, there's always the possibility of exploiting patterns specific to your code (some subranges are already sorted, frequently used specific small sequence sizes, etc). 也就是说,总是有可能利用特定于您的代码的模式(一些子范围已经排序,经常使用特定的小序列大小等)。

Two answers, probably generally-applicable: 两个答案,可能一般适用:

  • You will probably not be able to implement more efficient versions of algorithms that many other smart people have spent much more time optimizing. 您可能无法实现更高效的算法版本,许多其他智能人员花费了更多时间进行优化。 By virtue of time and testing alone, the STD algorithms will be pretty good. 凭借时间和测试,STD算法将非常好。

  • For identical algorithms, optimization is something which is "very hard" with all the current hardware and configuration variations. 对于相同的算法,优化是对所有当前硬件和配置变化“非常困难”的事情。 To give an example, the primary factor for algorithm performance on a particular platform might be which levels of cache its most frequently used routines can be stored in, which is not generally something you can optimize by hand. 举一个例子,特定平台上算法性能的主要因素可能是其最常用的例程可以存储在哪个级别的缓存中,这通常不是手工优化的。 Hence, the compiler is generally much more of a factor for actual algorithm performance than any particular code you can write. 因此,与您可以编写的任何特定代码相比,编译器通常更多地是实际算法性能的一个因素。

But yeah... if you're really serious about optimization, get down into the assembly and compare. 但是,是的......如果你真的认真对待优化,那么请进入组装并进行比较。 My advice, though, would be to focus on other things, unless it's your job to optimize your implementation or something. 不过,我的建议是专注于其他事情,除非你的工作是优化你的实现或其他什么。 Just my 2c. 只是我的2c。

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

相关问题 C++ 在哪里可以找到 gcc 编译器标准库的实现文件? - C++ where I can find the implementation files of the standard library for the gcc compiler? 在哪里可以将源代码下载到GNU C ++标准库? - Where can I download the source code to the GNU C++ Standard Library? 在哪里可以找到标准的 Visual C++ 头文件? - Where can I find the standard Visual C++ header files? 我在哪里可以看到C ++标准库中使用的代码? - Where can I see the code used in C++ standard libraries? 在哪里可以找到C ++标准库中特定功能的源代码? (Visual Studio安装) - Where to find source code for specific function in C++ standard library? (Visual Studio installation) 在哪里可以找到C ++ / MFC的供稿库? - Where can I find a feed Library for C++/MFC? 在哪里可以找到 C++ DNS 库? - Where can I find a C++ DNS Library? 在哪里可以找到当前的 C 或 C++ 标准文档? - Where do I find the current C or C++ standard documents? 在哪里可以找到 C++ 语言的标准 BNF 或 YACC 语法? - Where can I find standard BNF or YACC grammar for C++ language? 在c ++中,仅在全局范围内允许使用“表达式”来初始化全局对象。 在标准中哪里可以找到? - In c++, `expressions` are allowed in global scope only to initialize global objects. Where can I find this in the Standard?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM