当前位置:网站首页>RI Gai series: push of STD container_ Why is back slower than []
RI Gai series: push of STD container_ Why is back slower than []
2022-06-23 21:06:00 【mariolu】
Recently, in analyzing the flame diagram data of operators , Found more std::vector::push_back operation , I wonder if it can be optimized here .
A few facts must be known .vector The capacity of ( Memory ) Never less , Even call clear Method , Unless used swap Method .(C++11 Language provides shrink_to_fit Method repair .)STL vector Another thorny problem with is that there are many ways to build . It can be used new perhaps push_back.
So what's the difference ?
push_back Except to put the data push Into the container , Also, the container memory size Size for boundary check . If the container doesn't have space to store new elements , The container memory will also be expanded once . We all know that capacity expansion means that the container goes to the system to find a larger memory address , Then copy the elements . So it is often used here reserver To pre allocate memory , Avoid capacity expansion . And the operator operator[] Just do some address searching , Then fill the address with data . It doesn't look very safe .
And it's instant use reserver Reserved space ,push_back Additional condition checks will also be performed , And this kind of examination is operator[] There won't be . Besides , and push_back increase size value (reserve Just set up capacity), So it will be updated every time .
In short ,push_back Do more than do operator[]—— That's why it's slower ( More accurate ).
here stackoverflow Threads also discuss more extension points , One of the more interesting things is that compared with simple new After performing operator[], One extra time memset It will reduce the processing time later . This is related to the system memory page management mechanism .https://stackoverflow.com/questions/20168051/why-push-back-is-slower-than-operator-for-a-previously-allocated-vector
The code here has also been modified accordingly . Before knowing in advance the number of elements to insert , Do it in advance resize operation , And then use operator[]. Of course, it cannot be said that obvious system optimization can be achieved , But at least for this function , Some improvements have been made .
边栏推荐
- Teach you how to develop desktop applications with web pages
- WinDbg loads mex DLL analysis DMP file
- Use of paging components in fusiondesign
- How to deal with unclear pictures? How to deal with color balance?
- [golang] quick review guide quickreview (IV) -- functions
- Yukeng MySQL service installation error
- Spend small money to do big things: cloud function + cloud development leverages the practice of e-commerce promoting flexible architecture in CCTV evening party
- Want to open an account, is it safe to open an account on the stock Internet? Will the funds be cheated away?
- Setinterval stop
- Customize view to imitate today's headlines and like animation!
猜你喜欢

Use of the vs2022scanf function. An error is reported when using scanf - the return value is ignored: Solutions

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

Applet development framework recommendation
Implementing MySQL fuzzy search with node and express

FPGA based electromagnetic ultrasonic pulse compression detection system paper + source file

Yaokui tower in Fengjie, Chongqing, after its completion, will be the safety tower for Sichuan river shipping with five local scholars in the company
Application of JDBC in performance test

JS advanced programming version 4: generator learning
随机推荐
How does the fortress machine connect to the server? Novice must know operation steps
Spend small money to do big things: cloud function + cloud development leverages the practice of e-commerce promoting flexible architecture in CCTV evening party
JS chain call
JS five methods to judge whether a certain value exists in an array
JS delete object attribute
[golang] delving into strings -- from byte run string to unicode and UTF-8
【Golang】深究字符串——从byte rune string到Unicode与UTF-8
[SAP ABAP] call API interface instance
【Golang】怎么实现Go程序的实时热更新
Copilot - employee, your layoff notice has been delivered
JS mailbox regular expression
Realize vscode to write markdown documents + pictures to be automatically uploaded to Tencent cloud cos
. Net Core . Net FrameWork . Net 5
How do I view the server when I log in to the fortress machine? Operation guide for novice
QPS fails to go up due to frequency limitation of public network CLB bandwidth
[JS reverse hundred examples] anti climbing training platform for netizens question 6: JS encryption, environment simulation detection
How to process the text of a picture into a table? Can the text in the picture be transferred to the document?
. Net Core 3. X MVC built-in log extension log4net
Application of JDBC in performance test
【Debian】Debian使用笔记