Jonas
Jonas

Reputation: 7017

Store data structure c++

I have alot of data (7k lines) stored in this way:

#include <vector>
#include <deque>
std::deque<std::deque<std::vector<unsigned char>>> data =
{
  {{0}},
  {{1,0},
   {0,1}},
  {{1,1,0,0},
   {0,1,1,0},
   {1,0,0,1},
   {0,1,0,1},
   {0,0,1,1}},
  {{1,1,1,0,0,0},
   {0,1,1,1,0,0},
   {1,0,1,0,1,0},
   {0,1,1,0,1,0},
   ...
  {0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1}}
};

This makes my code run much faster since I can skip some parts. The length of the vectors does not need to be variable, but this was just a simple way. The problem is that it now takes in the matter of minutes, instead of seconds to compile.

Is there a better way to store this data? Or should it be loaded at run time?

If it is loaded at run time, won't it just take time there?

Upvotes: 1

Views: 451

Answers (2)

J&#248;rgen Fogh
J&#248;rgen Fogh

Reputation: 7656

You should start by thinking about what you are trying to achieve. Are you afraid that your program will feel sluggish to the user?

Loading 7kB of data from a hard drive will be faster than any human will be able to notice. My guess is that it would be fine for almost any purpose. If the data can be compiled in, it must be constant, which means that it will only ever have to be loaded once anyway. Even safety critical systems with hard deadlines can usually afford a few milliseconds during program initialization.

In the unlikely case that this even matters, you should measure the time it takes to load the data and see if it is acceptable for your purpose.

Upvotes: 2

Olipro
Olipro

Reputation: 3529

Look into whether or not your compiler supports a pre-compiled header and if so, make this data part of that PCH. This will result in the slow work only occuring when the data actually changes and will separate it out from all your other code changes.

If, for some reason a PCH isn't an option, you could also create a header that extern's your std::vector and have a single .cpp file containing your ~7k lines of elements. Most compilers won't recompile a single unit if the file is unmodified.

Upvotes: 4

Related Questions