当前位置:网站首页>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 .
边栏推荐
- [golang] use go language to operate etcd - configuration center
- Script tag attributes and & lt; noscript> label
- Memory patch amsi bypass
- How do I view the server when I log in to the fortress machine? Operation guide for novice
- How to deal with product pictures? How to select mapping software?
- 【Golang】怎样优雅的清空切片
- 打新债到底是用什么软件比较安全?打新债平台有哪些
- [golang] quick review guide quickreview (II) -- slice
- Machine learning related
- What is the process of setting up local cloud on demand? Can cloud on demand audit videos?
猜你喜欢

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

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

Applet development framework recommendation
Application of JDBC in performance test

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

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

JS advanced programming version 4: generator learning
Implementing MySQL fuzzy search with node and express
随机推荐
CPS 22 January additional incentive rules
[golang] quick review guide quickreview (x) -- goroutine pool
JS chain call
Do you need a server to set up cloud on demand? What services can cloud on demand provide?
Digital procurement transformation solution: SaaS procurement management platform promotes enterprise sunshine procurement
JS to get the screen size, current web page and browser window
Spend small money to do big things: cloud function + cloud development leverages the practice of e-commerce promoting flexible architecture in CCTV evening party
[golang] quick review guide quickreview (VI) -- struct
How to deal with unclear pictures? What are the techniques for taking clear pictures?
. Net cloud native architect training camp (rgca four step architecture method) -- learning notes
3000 frame animation illustrating why MySQL needs binlog, redo log and undo log
How to build a cloud game platform on the server? How to select a cloud game server?
Newbeecoder. UI new open source control library DataGrid instructions
From AIPL to grow, talking about the marketing analysis model of Internet manufacturers
Advantages of short video automatic audit? What are the difficulties of manual audit?
[golang] use go language to operate etcd - configuration center
Summary of multiple methods for obtaining the last element of JS array
What is the role of computer auto audit audio? What content failed to pass the audit?
[sap-hcm] report jump to pa30/pa40 instance
. Net Core . Net FrameWork . Net 5