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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:28:46+00:00 2026-05-11T14:28:46+00:00

I have a class containing a number of double values. This is stored in

  • 0

I have a class containing a number of double values. This is stored in a vector where the indices for the classes are important (they are referenced from elsewhere). The class looks something like this:

Vector of classes

class A {   double count;   double val;   double sumA;   double sumB;    vector<double> sumVectorC;   vector<double> sumVectorD; }  vector<A> classes(10000); 

The code that needs to run as fast as possible is something like this:

vector<double> result(classes.size()); for(int i = 0; i < classes.size(); i++) {   result[i] += classes[i].sumA;   vector<double>::iterator it = find(classes[i].sumVectorC.begin(), classes[i].sumVectorC.end(), testval);   if(it != classes[i].sumVectorC.end())     result[i] += *it; } 

The alternative is instead of one giant loop, split the computation into two separate loops such as:

for(int i = 0; i < classes.size(); i++) {   result[i] += classes[i].sumA; } for(int i = 0; i < classes.size(); i++) {  vector<double>::iterator it = find(classes[i].sumVectorC.begin(), classes[i].sumVectorC.end(), testval);   if(it != classes[i].sumVectorC.end())     result[i] += *it; } 

or to store each member of the class in a vector like so:

Class of vectors

vector<double> classCounts; vector<double> classVal; ... vector<vector<double> > classSumVectorC; ... 

and then operate as:

for(int i = 0; i < classes.size(); i++) {   result[i] += classCounts[i];   ... } 

Which way would usually be faster (across x86/x64 platforms and compilers)? Are look-ahead and cache lines are the most important things to think about here?

Update

The reason I’m doing a linear search (i.e. find) here and not a hash map or binary search is because the sumVectors are very short, around 4 or 5 elements. Profiling showed a hash map was slower and a binary search was slightly slower.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-11T14:28:47+00:00Added an answer on May 11, 2026 at 2:28 pm

    As lothar says, you really should test it out. But to answer your last question, yes, cache misses will be a major concern here.

    Also, it seems that your first implementation would run into load-hit-store stalls as coded, but I’m not sure how much of a problem that is on x86 (it’s a big problem on XBox 360 and PS3).

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

Sidebar

Related Questions

I have an assembly containing a number of classes. This is a class library
I have a class library containing a number of classes, and in each class
I have a class containing a number of properties, something like: public class Update
Hi I have a hash of hash containing class name, number of students enrolled
I have a JTable instance, containing a number of rows. The columns in this
Say I have two classes class Driver{ //attributes of driver ,ex: driving licence number
So I have a list containing a number of instances of a class. As
I have a class containing a collection of items. For convenience I've provided GetCurrentItem
I have a class containing Linq To SQL objects that are used to populate
I have a class containing several properties (all are strings if it makes any

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.