Introduction to Stacks using std::stack

Performance considerations with std::stack

What are some performance considerations to keep in mind when using std::stack?

Illustration representing computer hardware

When using std::stack, there are several performance factors to consider:

Underlying Container

The choice of the underlying container can have a significant impact on performance. By default, std::stack uses std::deque, which provides good overall performance for most use cases. However, if you have specific requirements, you might consider using a different container:

  • std::vector: If you need the best possible memory locality and you don't mind potential reallocation costs when the stack grows, std::vector can be a good choice.
  • std::list: If you need to avoid reallocation costs and you don't mind the extra memory overhead of a linked list, std::list can be used.

Push and Pop Operations

std::stack is designed to provide constant time complexity (O(1)) for push and pop operations. This means that these operations are generally very fast. However, the actual performance will depend on the efficiency of the corresponding operations in the underlying container.

Copying and Moving Elements

When you push elements onto the stack, they are copied or moved. If your elements are expensive to copy or move, this can impact performance. Consider moving elements instead of copying them when possible.

Exception Handling

std::stack provides strong exception safety, which can have a slight performance cost. If you know that your code will never throw exceptions, you can use a custom stack implementation that omits exception handling for a small performance boost.

Stack Size

If you know in advance how many elements your stack will typically hold, you can reserve that capacity in the underlying container (if it supports it). This can prevent unnecessary reallocations as the stack grows.

Here's an example that demonstrates some of these considerations:

#include <chrono>
#include <iostream>
#include <stack>
#include <vector>

void TrackPerformance(size_t Reserve = 1) {
  using namespace std::chrono;

  // Create a vector with reserved capacity
  std::vector<int> vec;

  // Create a stack using the vector as
  // its underlying container
  std::stack<int, std::vector<int>> s(

  // Measure the time taken to push
  auto start = high_resolution_clock::now();
  for (int i = 0; i < 1'000'000; ++i) {
  auto end = high_resolution_clock::now();

  std::cout << "Time taken to push 1,000,000"
    " elements: " << duration_cast<milliseconds>(
      end - start).count() << " ms\n";

int main() {
Time taken to push 1,000,000 elements: 46 ms
Time taken to push 1,000,000 elements: 71 ms

In this code, we use std::vector as the underlying container and reserve a capacity of 1,000,000 elements upfront. This can prevent reallocations as we push elements. We then measure the time taken to push 1,000,000 elements.

Remember, performance optimizations should always be based on actual profiling and measurement in your specific use case. Premature optimization can often lead to more complex and harder-to-maintain code without providing meaningful benefits.

Answers to questions are automatically generated and may not have been reviewed.

Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved