Home > Backend Development > PHP Tutorial > C++ performance analysis (3): Heap Object vs. Stack (auto) Object, heapstack_PHP tutorial

C++ performance analysis (3): Heap Object vs. Stack (auto) Object, heapstack_PHP tutorial

WBOY
Release: 2016-07-13 10:20:09
Original
1295 people have browsed it

C++ Performance Analysis (3): Heap Object vs. Stack (auto) Object, heapstack

It is generally believed that the performance improvement is the 90 ~ 10 rule, that is, 10% Your code is responsible for 90% of performance problems. Programmers who have done large-scale software projects generally know this concept.

However, for software engineers, some performance problems are unforgivable. Whether they belong to 10% or 90%, they "must" be improved. Here I will talk about one of the issues: whether to use heap or stack.

Java, C#, and JavaScript programmers generally don't care whether the objects they create are in the heap or the stack, because for these languages, objects can only "live" in the heap. This is undoubtedly much simpler for programmers. But for C++ programmers, you can choose three places to create objects:

  • Data section of the program
  • Working Stack
  • Heap

Where should Object live? This problem must be determined by the properties of the application. Sometimes there is no choice. For example, for dynamically generated global variables, there is no other way but to live in the heap.

However, once we have a choice, such as a temporary object as a carrier of complex data, the answer is clear: stack should be preferred. For example, the following simple example:

    // heap vs stack test

double HeapVsStack(bool heap, int loop, int &result)

{

if (heap)

               {

clock_t begin = clock();

for(int i = 0; i < loop; ++i)

                                                                

intPair *p = new intPair(1,2); <🎜>

                                                                                   delete p;              } clock_t end = clock();

return double(end - begin) / CLOCKS_PER_SEC;          } else

                {

clock_t begin = clock();

for(int i = 0; i < loop; ++i)

                                                                

intPair p = intPair(1,2);

result += p.ip1 + p.ip2;

             } clock_t end = clock();

return double(end - begin) / CLOCKS_PER_SEC;

         }

}

The bold enlarged part of the program is the "application logic" to be measured. An intPair is created in the heap above and deleted after use. Below, define the same auto variable in the stack. There is no need to care after use.

Make the following simple test calls to this program:

int result = 0;

printf("Heap time: %f n", HeapVsStack(true, 100000, result));

printf("Stack time: %f n", HeapVsStack(false, 100000, result));

I had to call 100,000 times because the difference in their time-consuming is too big: the use case of stack shows 0ms for less than 10,000 times.

The test results show that the heap took 300ms and the stack took 5ms, a difference of 60 times.

Conclusion:

1) If application logic allows, use stack-based auto variables and never use heap variables.

2) If you need to use a lot of heap, it is recommended to use std::vector as your own simple heap manager. Avoid using heap directly and extensively to create ad-hoc objects.

3) Some classes used for temporary calculations may be prohibited from being generated in the heap. See http://stackoverflow.com/questions/1941517/explicitly-disallow-heap-allocation-in-c article.

Heap memory

What is the difference between them and Stack memory? Quote: www.blogjava.net/...9.aspxI was reading an article this morning that dealt with the problem of buffer overflow. When it came to the stack and heap of C, I didn’t understand something, so I searched Baidu and found the forum. The explanations on the above are relatively messy. After searching on Google, I found that there are many explanations in the Lecture Notes of foreign universities. Here are three simple excerpts, one is in C, the second is for Java, and the third is from the perspective of OS.

Stack vs Heap AllocationHow the memory of the computer is organized for a running program? When a program is loaded into memory, it is organized into three areas of memory, called segments: the text segment, stack segment, and heap segment. The text segment (sometimes also called the code segment) is where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system.
The remaining two areas of system memory is where storage may be allocated by the compiler for data storage. The stack is where memory is allocated for automatic variables within functions. A stack is a Last In First Out (LIFO) storage device where new storage is allocated and deallocated at only one ``end'', called the Top of the stack. This can be seen in Figure 14.13.

figure14.13.gif

When a program begins executing in the function main(), space is allocated on the stack for all variables declared within main(), as seen in Figure 14.13(a). If main() calls a function, func1 (), additional storage is allocated for the variables in func1() at the top of the stack as...The rest of the text>>


What is the difference between Heap memory and Stack memory?





The heap is dynamically allocated, such as malloc or new, while the stack is static.

And the location of the applied storage space is different.

http://www.bkjia.com/PHPjc/868462.html
www.bkjia.com
true


http: //www.bkjia.com/PHPjc/868462.html

TechArticle

C++ Performance Analysis (3): Heap Object vs. Stack (auto) Object, heapstack It is generally believed that the performance improvement is The 90 ~ 10 rule means that 10% of the code is responsible for 90% of the performance problems. Did...

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template