Recently posted comments on Weibo, involving "performance optimization" and C ++ bad habits.

Source: Internet
Author: User
Recently, Weibo's remarks are archived, involving "performance optimization" and C ++ bad habits.

Bad habits of Writing C ++ programs: Class names start with C, for example, cdate; member variables start with M _; variables use the Hungarian naming method; I don't know when to disable copy-ctor/assign operator. The first three may have been passed down from MFC. At that time, C ++, class, and OO were new things. They wanted to distinguish them from C struct. Now they are just doing this. The member variables of C ++ can be named in a special format and can be suffixed with an underscore (the prefix is incorrect ). However, in Java, it is not necessary to imitate the member variable naming method in C ++. IDE can display member variables in different colors and distinguish them from local variables without special names. When writing a program, I am afraid to apply the previous programming experience to the new language in a different way.

Knowing that disabling copy-ctor/assign operator is a touchstone for C ++ programmers. When I see an open-source project, I usually first check whether its raiI handle class has disabled copy-ctor/assign operator (such as thread, mutex, condvar, connection). If not, the first impression is poor.

For the class naming style, Google, llvm, Mozilla, and muduo all adopt the Pascal style (likethis), such as eventloop and sudokusolver. Coincidentally, both of them are indented by two cells. You can use clang-format to automatically format the code.

By the way, I do not agree with the two C ++ principles: 1. Replace null with nullptr, 2. Replace stdio. h with the cstdio header file.

For example, if a system function such as gettimeofday (& TV, null) is passed into the nullptr, it is really illegal. Now, using null can also achieve the benefits of nullptr. If it is a big deal, define it in a header file. This may change in the future.

In addition, the ctime header file does not define STD: gettimeofday, while time. h defines: gettimeofday. I don't want to back up functions in C language, POSIX header files in C language, and POSIX (in Linux, the two are basically not separated ). How many system functions (such as fcntl () Should I include cfcntl or fcntl. h? Is the thread cpthread or pthread. h? I can't remember the parameter sequence of memset (), so bzero () is generally used, but manpage says bzero () is declared in strings. H. Do I want to consider cstrings? Why bother yourself? The. h header file should be used for content outside the C ++ standard library.

Performance Optimization?

Some people often talk about "performance", and the "optimization measures" on the grounds of "Improving Performance" often fail to come up with ideas, which only increases complexity and maintenance difficulty, reduces code quality. This is an incorrect decision point. I found that beginners tend to focus too much on micro (statement-level) performance, for example, on which while (true) and for (;) are faster, ++ I and I = I + 1 are faster, i/= 16 and I> = 4 which are fast and so on, while ignoring the optimization capability of modern compilers.

Some people talk about performance optimization. First, they can't get specific and reasonable performance goals. They just want to get better and faster. Second, they cannot accurately measure and verify the performance data, and they will act based on their feelings and outdated experiences. When coding, both methods are feasible. The decision-making method is to select a "better performance" option based on the feeling, while ignoring other more important factors. The typical relationship between readability and performance is as follows: how many occasions is it worth sacrificing the readability and maintainability of the Code for performance? I want my code to be located in Zone 3rd, while some people think that my code is located in Zone 4th, which is actually located in Zone 1st.

Those who can write code in Area 4th are rare. Sometimes you have struggled to optimize it for a long time. As a result, the new CPU has added several commands and solved the problem directly on the hardware layer. Now, some people have to roll up their sleeves and write their own memory pool, claiming to be able to improve performance. When Ulrich drepper is really a commodity? (Section 12.2.8 "is it necessary to customize the memory distributor?") How do you plan to test the performance of the memory distributor (malloc? What are the indicators? What factors need to be controlled or simulated (such as the number of threads )? Does your test result reflect the actual scenario?

Miscellaneous

Someone asked why I said "Poco is not a server C ++ network library" (http://www.oschina.net/question/12_120943) although it also provides reactor? Because its Reactor uses Socket: Select (), although the latter encapsulates epoll, you can see its implementation that it creates and destroys epoll FD every time it is called, then re-create the entire watch list. No server network library will do this.

Well, there are two types of network programming in the world: Network Programming and Windows Network Programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.