C + + Performance profiling (iii): Heap Object vs. Stack (auto) object,heapstack
It is generally assumed that performance improvements are 90 to 10 rules, that is, 10% of the code is responsible for 90% performance issues. Programmers who have done large-scale software engineering generally know this concept.
For software engineers, however, some performance issues are inexcusable, whether they belong to 10% or 90%, and are "must" to be improved. Here's a question about whether to use heap or stack.
Java, C #, and JavaScript programmers generally don't have to worry about whether the object they create is in the heap or in the stack, because for these languages, object can only "live" in the heap. This is undoubtedly a lot easier for programmers. But for C + + programmers, you can choose three places to create an object:
- Data section of the program
- Work stack
- Heap
Object because where is the life? This problem must be determined by the attributes of the application, sometimes without choice, such as the dynamic generation of the whole variable, only live in the heap, there is no way.
However, once we have options, such as temporary, object as the 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 (n);
Result + = P->ip1 + p->ip2;
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;
result + = P.ip1 + p.ip2;
}
clock_t end = Clock ();
return double (end-begin)/clocks_per_sec;
}
}
The bold part of the program is the "application logic" to be measured, a intpair is created in the heap above, and the delete is deleted after the run. The same auto variable is defined in the stack below, so you don't need care when you're done.
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 their time-consuming differences were too great: the stack's use cases were displayed at less than 10,000 times for 0ms.
The test result, the heap uses 300ms, the stack uses 5ms, the difference is 60 times times.
Conclusion:
1) If the application logic allows, use stack-based auto variable, do not need the heap variable.
2) If you need a lot of heap, it is recommended to use std::vector as your own heap simple manager. Avoid using heap to create Ad-hoc object directly and extensively.
3) Some temporary computing classes can be considered to prohibit generation in the heap, see http://stackoverflow.com/questions/1941517/explicitly-disallow-heap-allocation-in-c article.
2014-8-23 Seattle
What is the difference between the Heap memory and the stack memory?
Reference: Www.blogjava.net/...9.aspx
Look at an article in the morning on the issue of buffer overflow, talking about the stack and heap of C, do not understand Baidu then found that the interpretation of the forum is more messy, Google after the discovery of foreign universities lecture notes have a lot of notes, the following simple excerpt three paragraphs, one is C, The second is for Java, and the third is from the OS point of view.
Stack vs Heap Allocation
How the memory of the computer are organized for a running program? When a program was loaded into memory, it was 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 was the machine language representation of the program steps to being carried out, including all functions making up the program, both user defined and system.
The remaining-areas of system memory is where storage could be allocated by the compiler for data storage. The stack is where memory was allocated for automatic variables within functions. A stack is a last in first out (LIFO) storage device where new storage are allocated and deallocated at only one ' end ', c Alled the Top of the stack. This can is seen in Figure 14.13.
Figure14.13.gif
When a program begins executing in the function main (), space was 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 ... Remaining full text >>
What is the difference between C heap memory and stack memory?
A heap is a dynamic application, such as malloc or new, and the stack is static.
And the location of the requested 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 profiling (c): Heap Object vs. Stack (auto) Object,heapstack generally believes that performance improvements are 90 to 10 rules, that is, 10% of the code is responsible for 90% performance issues. Have done sth .