Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of replacing iTunes + with + + I in C++

2025-04-09 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article is about the example analysis of replacing iTunes + with + + I in C++. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Static code analysis tools simplify the coding process, detect errors, and help fix them. PVS-Studio is a static code analysis tool for CCompact +. The team tested more than 200 Cramp Cure + open source projects, including well-known projects such as Unreal Engine, Php, Haiku, Qt, and Linux kernels. So they share an error case every day and give suggestions accordingly.

This bug is found in the source code of Unreal Engine 4.

Error code:

Void FSlateNotificationManager::GetWindows (TArray

< TSharedRef >

& OutWindows) const {for (auto Iter (NotificationLists.CreateConstIterator ()); Iter; Iter++) {TSharedPtr NotificationList = * Iter;.... }}

Explanation:

If you don't read the title, it may be difficult to find the problem in this code. At first glance, this code looks completely correct, but in fact it is not perfect. Yes, I mean the post-increment operator Iter++. We should try to use the pre-self-increment operator instead of the post-self-increment operator, that is, using + + Iter instead of Iter++. Why do you do this? what is the practical value? I will explain in detail below.

Correct code:

Void FSlateNotificationManager::GetWindows (TArray

< TSharedRef >

& OutWindows) const {for (auto Iter (NotificationLists.CreateConstIterator ()); Iter; + + Iter) {TSharedPtr NotificationList = * Iter;.... }}

Recommendations:

The difference between prefix and suffix forms is well known. I hope everyone knows the difference in their internal structure (which tells us the algorithm). If you have ever used operator overloading, you must be aware of it. If you haven't used it, I'll explain it briefly here (those who have used operator overloading can skip the following example of operator overloading).

The pre-self-increment operator changes the state of the object and returns the state after the object has changed, so there is no need to create a temporary object. Here is an example of the pre-self-increment operator:

MyOwnClass& operator++ () {+ + meOwnField; return (* this);}

The post-increment operator also changes the state of the object, but returns the state before the object was changed, and a temporary object needs to be created. The following is an example of post-increment operator overloading:

MyOwnClass operator++ (int) {MyOWnCLass tmp = * this; + + (* this); return tmp;}

If you look at the above code, you will find that there is an additional operation, which is to create a temporary object, which is too important in practice!

Today's compilers are very smart when doing code optimization, and if they are of no use, they will not create temporary objects casually. That's why it's hard to see the difference between iTunes + and + + I in the release.

But when debugging a program in debug mode, it's another matter, and you'll see a big difference in performance.

There are some examples that can estimate the running time of code using the pre-increment and post-increment operators in the debug version, and we can see that it takes almost four times as long to use the suffix form as the prefix.

Some people will say, "so what? anyway, the release version is the same." This kind of thinking is both right and wrong. Usually we spend more time doing unit testing and debugging programs, so we spend most of our time working in debug versions, and no one wants to waste time waiting there?

About "for iterators, should we use the pre-self-increment operator (+ + I) instead of the post-self-increment operator (iincrement +)?" I would like to answer this question seriously: "Yes, it should be done." You will find that the speed is greatly improved in the debug version. If the iterator is complex, the benefits are even more obvious.

This error was discovered using the static code analysis tool PVS-Studio with the error message: V803 performance degradation. If iter is an iterator, it is more efficient to use the pre-increment operator, using + + iter instead of iter++.

Thank you for reading! This is the end of the article on "the example analysis of replacing iTunes + with + + I in C++". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report