1. int
  2. shared_ptr int
  3. any int
  4. shared_ptr any int
테스트 합계 소요 시간 (ms) vector<int> 대비 배수
vector<int> 4,999,999,950,000,000 170.591 1.00x
vector<shared_ptr<int>> 4,999,999,950,000,000 696.45 4.08x
vector<any> (int) 4,999,999,950,000,000 874.228 5.12x
vector<any> (shared_ptr<int>) 4,999,999,950,000,000 1653.83 9.70x
// ...existing code...
#include <iostream>
#include <vector>
#include <any>
#include <chrono>
#include <memory>
#include <cstdlib>

class Timer
{
    using Clock = std::chrono::high_resolution_clock;
    Clock::time_point start_time;

public:
    Timer()
        : start_time(Clock::now())
    {
    }

    double elapsed() const
    {
        auto end_time = Clock::now();
        return std::chrono::duration<double, std::milli>(end_time - start_time).count();
    }
};

int main(int argc, char** argv)
{
    size_t DATA_SIZE = 1'000'000; // 기본 100만
    if (argc > 1)
    {
        DATA_SIZE = static_cast<size_t>(std::stoull(argv[1]));
    }

    std::cout << "데이터 크기: " << DATA_SIZE << "개\n\n";

    long long sum1 = 0;
    long long sum2 = 0;
    long long sum3 = 0;
    long long sum4 = 0;

    // 1) plain int
    {
        std::vector<int> v;
        v.reserve(DATA_SIZE);
        for (size_t i = 0; i < DATA_SIZE; ++i)
        {
            v.push_back(static_cast<int>(i));
        }

        Timer t;
        for (const auto& x : v)
        {
            sum1 += x;
        }
        std::cout << "[vector<int>] sum=" << sum1 << " time(ms)=" << t.elapsed() << "\n\n";
    }

    // 2) heap-allocated ints (shared_ptr으로 변경: copyable)
    {
        std::vector<std::shared_ptr<int>> v;
        v.reserve(DATA_SIZE);
        for (size_t i = 0; i < DATA_SIZE; ++i)
        {
            v.emplace_back(std::make_shared<int>(static_cast<int>(i)));
        }

        Timer t;
        for (const auto& p : v)
        {
            sum2 += *p;
        }
        std::cout << "[vector<shared_ptr<int>>] sum=" << sum2 << " time(ms)=" << t.elapsed() << "\n\n";
    }

    // 3) std::any storing int
    {
        std::vector<std::any> v;
        v.reserve(DATA_SIZE);
        for (size_t i = 0; i < DATA_SIZE; ++i)
        {
            v.emplace_back(static_cast<int>(i));
        }

        Timer t;
        for (const auto& a : v)
        {
            if (auto p = std::any_cast<int>(&a))
            {
                sum3 += *p;
            }
        }
        std::cout << "[vector<any> (int)] sum=" << sum3 << " time(ms)=" << t.elapsed() << "\n\n";
    }

    // 4) std::any storing shared_ptr<int> (copyable 타입 사용)
    {
        std::vector<std::any> v;
        v.reserve(DATA_SIZE);
        for (size_t i = 0; i < DATA_SIZE; ++i)
        {
            v.emplace_back(std::make_shared<int>(static_cast<int>(i)));
        }

        Timer t;
        for (const auto& a : v)
        {
            if (auto p = std::any_cast<std::shared_ptr<int>>(&a))
            {
                sum4 += *(*p);
            }
        }
        std::cout << "[vector<any> (shared_ptr<int>)] sum=" << sum4 << " time(ms)=" << t.elapsed() << "\n\n";
    }

    return 0;
}
// ...existing code...