C ++ Research: two performance optimizations that you may not consider during development, considering Performance Optimization

Source: Internet
Author: User

C ++ Research: two performance optimizations that you may not consider during development, considering Performance Optimization
Zookeeper

1. Excessive storage references may result in lower performance;

2: improve program performance with locality;

Let's talk about how the reference program reduces program performance. I personally think there are two main reasons to reduce program performance. One is that the data structure selection is unreasonable, and the other is that multi-layer nested loops lead to redundant code execution. In the second case, we generally optimize the code at the innermost layer of the loop. If we can extract the code to the outer layer as much as possible, we can optimize the running speed if we cannot.

1: redundant storage references cause performance degradation. Let's look at a question about the performance reduction caused by references. Which of the following two methods is faster.

static void Test2(ref int sum)        {            for (int i = 1; i <= timer; i++)            {                sum += i;            }        }        static void Test3(ref int sum)        {            int tmpSum = sum;            for (int i = 1; i <= timer; i++)            {                tmpSum += i;            }            sum = tmpSum;        }

Generally, there should be no difference in their performance, because the two methods actually use a loop summation, and the real performance that can affect the method is this loop, the loops of the two methods can be said to be the same on the surface. When I set timer to 10000000, I would calculate 1 + 2 +... + The sum of 10000000, the speed of Test3 is faster than test2. Yes, Test3 is faster than Test2. The larger the timer in a certain range, the larger the performance difference. The running structure is as follows:

Let's look at the disassembly code. Part of the disassembly code is as follows. We only need to look at the part in the red line.

The most important code is: sum + = I; the Test2 method has more than the last line than the Test3 method, and the sum obtained after each loop is written back to the memory, the Test3 method does not need to be so troublesome. It only uses one register, writes the obtained sum to the register, completes the sum, and writes the sum to the memory. In each loop, Test2 reads the memory twice (sum and I are read from the memory) and writes the memory once (write the obtained sum to the memory ), the Test3 method only needs to read the memory once, that is, to read the I value from the memory. The performance of Test3 is higher than that of test2. Because Test2 reads the sum value every time in reference mode, the CPU needs to get the sum value through the address of sum in the memory, so it must read the memory ,, test3 does not need to read the memory. Use a register.

Technical Improvement visit www.cgzhw.com game programming network very good technical site.

2: improve program performance with locality.Let's look at a simple example.

static int Test4(int[,] arr, int row, int column)        {            int sum = 0;            for (int i = 0; i < row; i++)            {                for (int j = 0; j < column; j++)                {                    sum += arr[i, j];                }            }            return sum;        }        static int Test5(int[,] arr, int row, int column)        {            int sum = 0;            for (int j = 0; j < column; j++)            {                for (int i = 0; i < row; i++)                {                    sum += arr[i, j];                }            }            return sum;        }

To put it simply, the two methods are almost identical. The difference is that Test4 sums by rows, while Test5 sums by columns. If the two methods are executed multiple times, the performance of Test4 is higher than that of test5. Run the two methods 100000 times and the results are as follows:

Why is the sum by row faster than that by column? In a simple sentence, arrays are saved by row. The CPU reads data from the memory each time. Instead of reading the data, it reads the data directly. Instead, it reads a high-speed cache row each time, that is, it reads more data each time, for example, if the required data is in the cache row, it does not need to be read in the memory. Instead, it is directly retrieved from the cache row, which is faster than reading from the memory. Here, the array is stored by row. That is to say, the CPU reads multiple array elements each time to export high-speed cache rows, if the required elements do not need to be retrieved from the memory in the high-speed cache row, this is the legendary hit rate. The hit rate based on the sum of rows is of course high. However, if you sum by column, the hit rate is naturally low. Each time the CPU reads multiple elements in a row into a high-speed cache row, only one element is required for column summation, that is to say, each time only one element is needed, but the CPU reads multiple elements, the hit rate is of course low, so low that the hit rate may be 0.

The program locality includes: spatial locality and temporal locality. Here we talk about spatial locality.

Spatial locality means that the data around the used data is likely to be used immediately.

Time locality means that a data that is used may be used again.


What are the advantages and disadvantages of developing a C/S architecture in various languages?

[Answer]
You can use multiple languages to develop the C/S architecture, including Java, C ++, C #, and Dephi. different architectures must follow the needs of your project, the following describes the advantages and disadvantages of JAVA and C ++ and introduces C.

The Java language is based on a Java Virtual Machine. Its biggest advantage lies in one compilation and running everywhere. It does not need to consider different system calls on various systems, all system calls can be completed through the underlying JVM encapsulation. Developers do not need to consider programming knowledge at the operating system level during the development process, for example, a Java application developed on windows can run on a Linux system. You only need to give it the corresponding Java virtual machine, at the same time, the advantage of Java lies in its open source and encapsulation. Java provides powerful class library support, including multithreading, data structure, socket programming, etc, developers can focus more on programming, rather than implementing some underlying applications. Java also provides a garbage collection mechanism. As we all know, in some languages such as C ++ that require memory maintenance, how to allocate and release memory is a headache for programmers, especially in a service framework, minor program leaks may cause huge problems. Java's garbage collection mechanism helps programmers solve this problem. At the same time, Java has some good application frameworks that can be used to quickly build network applications. For example, J2EE is a good choice for enterprise-level development.

C ++ is characterized by its efficiency. It is a language that deals directly with the underlying layer. As the landlord said, most of online games are developed using c ++, because games require high operation efficiency and good user experience, this Java program does not support well, in addition, some very demanding algorithms are often developed using lower-layer languages such as C and assembly. The feature of C ++ lies in its object-oriented encapsulation, which is similar to that of Java, but it has the fast and efficient features of C language, when searching for projects, C ++, C ++, and C are usually used. They encounter some problems in the development of cross-platform projects, it mainly comes from the differences between the support of various operating systems and underlying api calls.

Let's talk about C #. This language is a platform launched by Microsoft this year. It is built on. on the NET Framework, the good compatibility of C # On Visual Studio allows C # To get started quickly. Due to its rich control library, C # language is favored by many developers. At the same time, C # language is very convenient for making C/S programs. Many codes are directly generated by IDE. Developers only need to implement part of the logic, but its disadvantages are also obvious..

For supplementary questions:
[Answer]
If you want to learn computer science, my advice is to first learn the relevant mathematical knowledge, including discrete mathematics. This knowledge is the foundation of computer science. At the same time, mastering one or two languages is necessary, for beginners, it is recommended to learn the C language. On the one hand, the C language is closer to the underlying layer. You can learn C to learn a lot about the underlying knowledge of the system. At the same time, because of its simple syntax, It is faster to get started, in addition, there are not many class libraries similar to Java and C # in C language, so that beginners can look more transparent and clean, and better understand from the language perspective. At the same time, I think that no matter what language you learn, algorithms and data structures in computer science are the most important. Language is the surface layer, and algorithms and data structures are the core, if you have mastered the essence, you will be able to remain unchanged. It is recommended that you master these two basic disciplines while learning the language. As you study deeply, you will find that all operations are built on them. At the same time, you have mastered the C language. In turn, it is easy to learn both Java and C #. It is nothing more than a conversion of expression languages. Finally, I personally think that if you only want to become a code writer, learning Java and C # is sufficient, because their Quick Start and powerful class libraries can help developers quickly implement them ...... remaining full text>

Answers questions about System Optimization

XP Acceleration
Acceleration Plan
The startup of WinXP has many functions that affect the speed. Although ms has been optimized, there are still many customizable features for us. I usually do this.
1. Modify the run key of the Registry to cancel the uncommon things, such as Windows Messenger. Enable Registry Manager: Start → Run → Regedit → find the key value "HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Run \ MSMSGS"/BACKGROUND, right click → Delete, the world is much quieter. By the way, let's take all the cfmon.
2. Modify the Registry to reduce the number of pre-reads and the wait time of the progress bar. The result is that the logon screen is displayed after the progress bar is circled. Start → run → regedit to start the Registry Editor, find HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Memory Management \ PrefetchParameters, and change the value of EnablePrefetcher to "1. In addition, friends who do not often change hardware can set the device type on the bus device to none (none) in the system property ).
3. Disable special effects in system properties, which is a simple and effective way to speed up. Click "start"> "Control Panel"> "system"> "advanced"> "performance"> "set to" optimal performance ">" OK. In this way, the desktop will be very similar to win2000. I still like the blue windows of XP, so I checked "use visual styles in windows and buttons, in this way, you can see the beautiful blue interface and speed up the process.
4. I use Windows commadner + Winrar to manage files. The ZIP support of Win XP is not as good as that of me. No matter what I need, I will open a zip support when I start the system, the idle system resources are less than one point. Click Start> Run and enter "regsvr32/u zipfldr. dll "in the middle of double quotation marks, and then press enter to confirm the success. A Prompt window appears, with the content roughly zipfldr. dll UnrgisterServer succeeded.
5. Quick browsing of Local Area Network Sharing
Generally, when Windows XP connects to another computer, it comprehensively checks all the tasks on the other computer. This check will allow you to wait for 30 seconds or more. The removal method is start → run → Regedit → locate HKEY_LOCAL_MACHINE \ Software \ Microsoft \ Windows \ Current Version \ Explorer \ RemoteComputer \ NameSpace in the registry. Under this key value, there will be a {D6277990-4C6A-11CF-8D87-00AA0060F5BF} key, delete it, restart the computer, Windows XP will no longer check the scheduled task, hoho ~~~ The speed has improved significantly!
6. Disable the debugger Dr. Watson.
I didn't seem to have used this item since win95. I can cancel it like this: Open the table and find the HKEY_LOCAL_MACHINE \ SOFTWARE \ Microsoft \ Windows NT \ CurrentVersion \ AeDebug sub-key branch, double-click the Auto key value name under it, change its "value data" to 0, and press F5 to refresh the settings to make the settings take effect. In this way, the operation is canceled. Following this idea, we can select all the debugging functions... the remaining full text>

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.