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 8994653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:28:50+00:00 2026-06-15T23:28:50+00:00

Is this a fair test for comparing a vector with an array? The difference

  • 0

Is this a fair test for comparing a vector with an array? The difference in speed seems too large. My test suggests the array is 10 to 100 times faster!

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <windows.h>
#include <stdint.h>

using namespace std;

double PCFreq = 0.0;
__int64 CounterStart = 0;

using namespace std;

void StartCounter()
{
    LARGE_INTEGER li;
    if(!QueryPerformanceFrequency(&li))
    std:cout << "QueryPerformanceFrequency failed!\n";

    PCFreq = double(li.QuadPart)/1000000000;

    QueryPerformanceCounter(&li);
    CounterStart = li.QuadPart;
}
double GetCounter()
{
    LARGE_INTEGER li;
    QueryPerformanceCounter(&li);
    return double(li.QuadPart-CounterStart)/PCFreq;
}

int _tmain(int argc, _TCHAR* argv[])
{
    //Can do 100,000 but not 1,000,000
    const int vectorsize = 100000;
    cout.precision(10);

    StartCounter();
    vector<int> test1(vectorsize);
    for(int i=0; i<vectorsize; i++){
        test1[i] = 5;
    }
    cout << GetCounter() << endl << endl;


    StartCounter();
    int test2[vectorsize];
    for(int i=0; i<vectorsize; i++){
        test2[i] = 5;
    }
    cout << GetCounter() << endl << endl;

    cout << test2[0];

    int t = 0;
    cin >> t;
    return 0;
}
  • 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-15T23:28:51+00:00Added an answer on June 15, 2026 at 11:28 pm

    It depends on what you are comparing.

    Your benchmark measures both setup time and access times together. It’s doubtless that std::vector has a more expensive setup time. This is because it needs to allocate memory, and then (by necessity of the standard) call default constructors on all the elements. Which for a POD type, means zeroing.

    So if you’re trying to measure access times, then no your benchmark isn’t accurate.

    Here’s some numbers to digest:

    Original Code:

    StartCounter();
    vector<int> test1(vectorsize);
    
    for(int i=0; i<vectorsize; i++){
        test1[i] = 5;
    }
    cout << GetCounter() << endl << endl;
    

    Time: 444353.5206


    Start timing after declaring and initializing the vector:

    vector<int> test1(vectorsize);
    
    StartCounter();
    for(int i=0; i<vectorsize; i++){
        test1[i] = 5;
    }
    cout << GetCounter() << endl << endl;
    

    Time: 15031.76101


    And for the array:

    StartCounter();
    int test2[vectorsize];
    for(int i=0; i<vectorsize; i++){
        test2[i] = 5;
    }
    cout << GetCounter() << endl << endl;
    

    Time: 38129.345

    The times are about the same regardless of whether the declaration is timed. This is likely because stack allocation is done all at once upon entry to the function.


    Basically, the vector memory allocation and initialization is taking a disproportionate amount of time. But the actual loop is fast.

    I’ll also note that your current benchmark framework is still sightly flawed. You only make one pass over each array. So cache-effects and lazy-allocation will be significant.

    The reason why the array is now slower is likely due to lazy-allocation. The array is allocated, but it hasn’t been committed yet. Lazy allocation means that it is committed upon first access – which involves a page-fault and a context-switch to the kernel.


    Here’s a fairer test with an outer loop to increase the benchmark time:

    vector<int> test1(vectorsize);
    
    StartCounter();
    for (int c = 0; c < 10000; c++){
        for(int i=0; i<vectorsize; i++){
            test1[i] = 5;
        }
    }
    cout << GetCounter() << endl << endl;
    

    Time: 227330454.6

    int test2[vectorsize];
    memset(test2,0,sizeof(test2));
    
    StartCounter();
    for (int c = 0; c < 10000; c++){
        for(int i=0; i<vectorsize; i++){
            test2[i] = 5;
        }
    }
    cout << GetCounter() << endl << endl;
    cout << test2[0];
    

    Time: 212286228.2

    So no an array is NOT faster than a vector for steady-state access. It’s just tricky to benchmark properly.

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

Sidebar

Related Questions

I've completed this set up on a fair few IIS 6 boxes, but one
I am running this code and it is using a fair amount of CPU
I have tried to figure this out and googled through a fair few stack
I've seen my fair share of IE stuff, but never seen this before &
From this question How to understand the non-fair mode of ReentrantReadWriteLock? , I think
I have this code: <div id=star-rating> <script type=text/javascript> $(function(){ $('#star-rating form').submit(function(){ $('.test',this).html(''); $('input',this).each(function(){ if(this.checked)
I have a page that is accessed via a URL like this: http://power-coder.net/Test/something.php?id=3#Page1 I
I seem to be doing this a fair bit in my code: public class
Possible Duplicate: Why use getters and setters? I see this a fair bit in
This may be a really long stretch but would make life a fair bit

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.