C + + Performance profiling (iii): Heap object vs. Stack (Auto) object

Source: Internet
Author: User

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 11th same auto variable is placed in the stack without 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 they were so different that the stack's use case was shown 0ms more than 10,000 times.

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 a large heap is required, it is recommended to use std::vector as your own heap 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

C + + Performance profiling (iii): Heap object vs. Stack (Auto) object

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.