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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:13:15+00:00 2026-06-17T19:13:15+00:00

I just started implementing my own vector class and i’m testing it with a

  • 0

I just started implementing my own vector class and i’m testing it with a simple file to check the time it takes to finish. One test took 2:30 minutes while the others took 90 and 29 seconds.

Something is hitting the performance of this class. Can you help me trace the source?

The test:

#include "MyVector.h"

const unsigned int SIZE_V= 1000000;
const unsigned int RUNS= 10000;

int main() {

      MyVector v(SIZE_V);

      for (unsigned int j=0; j<RUNS; ++j) {
        for (unsigned int i=0; i<SIZE_V; ++i) {
          v[i]= i;
        }
      }

      return 0;
}

The class:

MyVector.h:

#ifndef MY_VECTOR_H
#define MY_VECTOR_H

class MyVector {

 public:

      MyVector(unsigned int size);
      ~MyVector();

      int& operator[](unsigned int i);

 private:
      int* _data;
      unsigned int _size;
      MyVector(const MyVector&);
      MyVector& operator=(const MyVector&);

};
#endif

MyVector.cpp:

#include "MyVector.h"
#include <assert.h>

MyVector::MyVector(unsigned int size) : _data(new int[size]) {
}

MyVector::~MyVector() {
      delete[] _data;
}

int& MyVector::operator[](unsigned int i) {
      assert(i<_size);
      return _data[i];
}

EDIT:

These are the test results:

granularity: each sample hit covers 4 byte(s) for 0.04% of 27.09 seconds

index % time    self  children    called     name
                                                 <spontaneous>
[1]    100.0   12.51   14.58                 main [1]
               11.28    0.00 1410065408/1410065408     MyVector::operator[](unsigned int) [2]
                3.31    0.00       1/1           MyVector::~MyVector() [3]
                0.00    0.00       1/1           MyVector::MyVector(unsigned int) [7]
-----------------------------------------------
               11.28    0.00 1410065408/1410065408     main [1]
[2]     41.6   11.28    0.00 1410065408         MyVector::operator[](unsigned int) [2]
-----------------------------------------------
                3.31    0.00       1/1           main [1]
[3]     12.2    3.31    0.00       1         MyVector::~MyVector() [3]
-----------------------------------------------
                0.00    0.00       1/1           main [1]
[7]      0.0    0.00    0.00       1         MyVector::MyVector(unsigned int) [7]
-----------------------------------------------
  • 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-17T19:13:16+00:00Added an answer on June 17, 2026 at 7:13 pm

    One thing you might want to do is make operator[] inline. When I do this, the performance of your code on my box improves threefold from

    real    0m18.270s
    

    to

    real    0m6.030s
    

    In the latter test, each iteration of the test loop takes about 0.6ns (!) or about 1.5 clock cycles.

    This is on a Sandy Bridge box using g++ 4.7.2 with -O3.

    P.S. There’s a bug in the code: the constructor doesn’t initialize _size, so the assert() has undefined behaviour.

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

Sidebar

Related Questions

I just have started to learn ASP.NET and implementing simple application with one GridView
I have been looking at using TDD and implementing proper testing (only just started
I just started implementing a share functionality but was wondering if its possible to
I have just started implementing ISet 's instead of IList 's in my project
I'm just getting started defining and implementing external javaScript libraries and I'm a little
I have started writing my own WebDAV server class in .NET, and the first
I have just started implementing signal listeners in a django project. While I understand
For one of my course project I started implementing Naive Bayesian classifier in C.
I just started working with OpenGL, but I ran into a problem after implementing
I'm just starting to mess with bindings. I've started implementing a preference dialog, binding

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.