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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:48:44+00:00 2026-05-16T02:48:44+00:00

I have an array of precomputed integers, it’s fixed size of 15M values. I

  • 0

I have an array of precomputed integers, it’s fixed size of 15M values. I need to load these values at the program start. Currently it takes up to 2 mins to load, file size is ~130MB. Is it any way to speed-up loading. I’m free to change save process as well.

std::array<int, 15000000> keys;

std::string config = "config.dat";

// how array is saved
std::ofstream out(config.c_str());
std::copy(keys.cbegin(), keys.cend(),
  std::ostream_iterator<int>(out, "\n"));

// load of array
std::ifstream in(config.c_str());
std::copy(std::istream_iterator<int>(in),
  std::istream_iterator<int>(), keys.begin());
in_ranks.close();

Thanks in advance.

SOLVED. Used the approach proposed in accepted answer. Now it takes just a blink.

Thanks all for your insights.

  • 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-16T02:48:44+00:00Added an answer on May 16, 2026 at 2:48 am

    You have two issues regarding the speed of your write and read operations.

    First, std::copy cannot do a block copy optimization when writing to an output_iterator because it doesn’t have direct access to underlying target.

    Second, you’re writing the integers out as ascii and not binary, so for each iteration of your write output_iterator is creating an ascii representation of your int and on read it has to parse the text back into integers. I believe this is the brunt of your performance issue.

    The raw storage of your array (assuming a 4 byte int) should only be 60MB, but since each character of an integer in ascii is 1 byte any ints with more than 4 characters are going to be larger than the binary storage, hence your 130MB file.

    There is not an easy way to solve your speed problem portably (so that the file can be read on different endian or int sized machines) or when using std::copy. The easiest way is to just dump the whole of the array to disk and then read it all back using fstream.write and read, just remember that it’s not strictly portable.

    To write:

    std::fstream out(config.c_str(), ios::out | ios::binary);
    out.write( keys.data(), keys.size() * sizeof(int) );
    

    And to read:

    std::fstream in(config.c_str(), ios::in | ios::binary);
    in.read( keys.data(), keys.size() * sizeof(int) );
    

    —-Update—-

    If you are really concerned about portability you could easily use a portable format (like your initial ascii version) in your distribution artifacts then when the program is first run it could convert that portable format to a locally optimized version for use during subsequent executions.

    Something like this perhaps:

    std::array<int, 15000000> keys;
    
    // data.txt are the ascii values and data.bin is the binary version
    if(!file_exists("data.bin")) {
        std::ifstream in("data.txt");
        std::copy(std::istream_iterator<int>(in),
             std::istream_iterator<int>(), keys.begin());
        in.close();
    
        std::fstream out("data.bin", ios::out | ios::binary);
        out.write( keys.data(), keys.size() * sizeof(int) );
    } else {
        std::fstream in("data.bin", ios::in | ios::binary);
        in.read( keys.data(), keys.size() * sizeof(int) );
    }
    

    If you have an install process this preprocessing could also be done at that time…

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

Sidebar

Related Questions

I have a two dimensional array that I need to load data into. I
I have array (vector) r =[2 4] and report matrix with size 50*2. I
I have array and contains 50 values , example [ 'london', 'bluehit', 'green', 'lonpark',
I have array of keywords. How can I find if any of these keywords
I have array of objects: var aoo = [{},{},{},....{},{},{}]; I need a optimized function
I have array $arr=array(a=>array(b=>123)) and I need to do with it something like this:
I have array of 0's and 1's. And I need a function which return
Imagine I have array of values ranging from 1 to 24. I want to
Suppose you have a sorted range (x to y) of values in an array.
I have array with int values int[] myArr = (int[]) alItems.ToArray(typeof (int)); index =

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.