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

  • Home
  • SEARCH
  • 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 6389623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T03:25:37+00:00 2026-05-25T03:25:37+00:00

Solution 1: If i have a class like, class car{ public: int a; string

  • 0

Solution 1:
If i have a class like,

class car{ public: int a; string b; bool c;};

i can build a vector of 200 cars:

std::vector<car>   allcas;  
allcars.resize(200)

at runtime, i just do:

this_car=allcars[102];

then ….

Solution 2:

i have

std::vector<int> a; a.resize(200);
std::vector<string>b; b.resize(200);
std::vector<bool> c; c.resize(200);

this_car_a = a[102];
this_car_b = b[102];
this_car_c = c[102];

Question:
Which one is faster?

Does anyone have an idea? thanks a lot in advance!

  • 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-05-25T03:25:38+00:00Added an answer on May 25, 2026 at 3:25 am

    A “struct of vectors” has a couple of advantages over a “vector of structs”:

    • If your inner loop doesn’t use every element of the struct, then struct-of-vectors can save on memory bandwidth, as unused element vectors will not be loaded into cache.
    • It is easier to vectorize. A struct-of-vectors may enable you to use the vector processing instructions of your processor (through assembly, intrinsics, or clever compilers) to speed up your inner loops.

    On the other hand, premature optimization is the root of all evil:

    • Using a struct-of-vectors is more difficult, awkward, and obscure.
    • You generally don’t know where your performance bottlenecks are until you’ve got your code up and running. Is it worth making your code more verbose, fragile, and difficult? You won’t know until you actually profile it.
    • The benefits of struct-of-vectors programming vary on a case by case basis. It doesn’t always yield a speedup; you could actually end up with worse performance.
    • In particular, if your access pattern is random (as opposed to sequential or otherwise localized) a struct-of-vectors organization could end up loading much more useless data from memory, if each cache line includes elements from multiple nearby objects…

    So, my recommendation is to use vector-of-structs by default, but keep struct-of-vectors in mind as an alternative (i.e., make sure you can switch later, if you expect sequential/local access patterns and it doesn’t cost much effort up front). Once your program is running, you can profile it to see where the performance-critical sections are, and try out struct-of-vector and vectorized operations where they’ll do the most good.

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

Sidebar

Related Questions

i have e.g. a class like: public class Person { public string name {
I have a class like this: public class test { public virtual int Id
i hope that i mean my words. i have a class like this: public
I have a class hierarchy that looks like this: class Base<TElement> { public TElement
I have extended a class like so: public class CormantRadDock : Telerik.Web.UI.RadDock { public
public class Car { public string SomeProperty { get; set; } public Manufacturer Manufacturer
I have a class like this: class A { public $var = ; function
Assume I have a class like this: public class Foo { public Bar RequiredProperty
I have a class which looks something like this: class MyClass { public: //
I have defined an Action pure abstract class like this: class Action { public:

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.