Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8458005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T12:57:41+00:00 2026-06-10T12:57:41+00:00

I was looking at the new chrono library (C++11) and trying to use it.

  • 0

I was looking at the new chrono library (C++11) and trying to use it. I wrote the two following programs:

vector.cpp

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

int main()
{
    std::vector<double> vector(1000000, 0.);

    auto start = std::chrono::high_resolution_clock::now();
    for(int i(0); i < vector.size(); i++)
    {
        vector[i] += 1.;
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::cout << "Elapsed time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl;

    return 0;
}

array.cpp

#include <iostream>
#include <array>
#include <algorithm>
#include <chrono>

int main()
{
    std::array<double, 1000000> array;

    std::fill(array.begin(), array.end(), 0.);

    auto start = std::chrono::high_resolution_clock::now();
    for(int i(0); i < array.size(); i++)
    {
        array[i] += 1.;
    }
    auto end = std::chrono::high_resolution_clock::now();

    std::cout << "Elapsed time: " << std::chrono::duration_cast<std::chrono::milliseconds>(end-start).count() << " milliseconds" << std::endl;

    return 0;
}

I obtained 9 millisecond for the array program and 12 milliseconds for the vector program. The std::vector seems about 33% slower than the std::array. I’m doing it right? Why this difference?

Ps: I’m using GCC 4.7, Mac OS X 10.7.

g++-mp-4.7 -std=c++11 vector.cpp -o vector
g++-mp-4.7 -std=c++11 array.cpp -o array
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T12:57:43+00:00Added an answer on June 10, 2026 at 12:57 pm

    I changed your code to this:

    std::array<double, 1000000> array;
    
    double total = 0;
    std::fill(array.begin(), array.end(), 0.);
    
    for (unsigned j = 0; j < 1000; ++j)
    {
        auto start = std::chrono::high_resolution_clock::now();
    
        for (unsigned i = 0; i < array.size(); i++)
        {
            array[i] += 1.;
        }
    
        auto end = std::chrono::high_resolution_clock::now();
        total = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    }
    
    std::cout << total << " for Array." << std::endl;
    
    std::vector<double> vector(1000000, 0.);
    total = 0;
    
    for (unsigned j = 0; j < 1000; ++j)
    {
        auto start = std::chrono::high_resolution_clock::now();
    
        for (unsigned i = 0; i < vector.size(); i++)
        {
            vector[i] += 1.;
        }
    
        auto end = std::chrono::high_resolution_clock::now();
        total = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
    }
    
    std::cout << total << " for Vector." << std::endl;
    

    My results using -O3:

    8123 for Array.
    8117 for Vector.
    

    Seems to me that both are equally fast.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: public void run() { try { logger.info(Looking for new
I'm looking at a new project and we are wanting to use Flex (to
I was looking for new strategies to manage the localization of my future apps,
So I was looking for new features in .Net 4.5 and I found that
Looking at the new VB 2010 features, I stumbled upon support for Auto-Implemented Properties
Currently looking into learn new technology and silverlight is on the potential list. However,
I'm looking to add new states to a TextView. I chose TextViews because they're
I've been looking at the new features in C++11 and it really looks like
I am not looking to create new events. I need to create a canvas
I've been looking at the new Scala 2.9 parallel collections and am hoping to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.