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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:37:58+00:00 2026-06-17T18:37:58+00:00

Possible Duplicate: Writing a binary file in C++ very fast I have a large

  • 0

Possible Duplicate:
Writing a binary file in C++ very fast

I have a large number of unsigned 32 bit integers in memory (1.5 billion entries). I need to write them to a file and read them back into main memory.

Now, I do it using:

ofstream ofs;
ofs.open(filename);
for (uint64_t i = 0 ; i < 1470000000 ; i++)
ofs << integers << " " ;

and

ifstream ifs;
ifs.open(filename);
for (uint64_t i = 0 ; i < 1470000000 ; i++)
ifs >> integers ;

This takes a few minutes to execute. Can anybody help me, is there any library method to do it in a faster way? Or any suggestion, so I can run a performance test? Can anybody show me some simple C++ code that uses mmap for doing the above (on Linux)?

EDIT: EXAMPLE CASE

#include<iostream>
#include <stdint.h>
#include <cstdio>
#include <cstdlib>
#include <sstream>

using namespace std;

main()
{
      uint32_t* ele = new uint32_t [100] ;
      for(int i = 0; i < 100 ; i++ )
      ele[i] = i ;

      for(int i = 0; i < 100 ; i++ ){
          if(ele[i] < 20)
          continue ;
          else
          // write  ele[i] to file
          ;   
      }

 for(int i = 0; i < 100 ; i++ ){
          if(ele[i] < 20)
          continue ;
          else
          // read  number from file
          // ele[i] = number * 10 ;
          ;   
      }

     std::cin.get();
}
  • 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-17T18:37:59+00:00Added an answer on June 17, 2026 at 6:37 pm

    The first thing to do is to determine where the time is going.
    Formatting and parsing text isn’t trivial, and can take some
    time, but so can the actual writing and reading, given the size
    of the file. The second thing is to determine how “portable”
    the data have to be: the fastest solution is almost certainly to
    mmap (or its Windows equivalent) the array to the file
    directly, and never read or write. This doesn’t provide
    a portable representation, however, and even upgrading the
    compiler might make the data unreadable. (Unlikely for 32 bit
    integers today, but it has happened in the past).

    In general, if the time is going to reading and writing, you
    will want to investigate using mmap. If it is going to
    formatting and parsing, you will want to investigate some sort
    of binary format—this could also help reading and writing
    if it makes the resulting files smaller. The simplest binary
    format, writing the values using the normal network standard,
    requires no more than:

    void
    writeInt( std::ostream& dest, int32_t integer )
    {
        dest.put( (integer >> 24) & 0xFF );
        dest.put( (integer >> 16) & 0xFF );
        dest.put( (integer >>  8) & 0xFF );
        dest.put( (integer      ) & 0xFF );
    }
    
    int32_t
    readInt( std::istream& source )
    {
        int32_t results = 0;
        results  = source.get() << 24;
        results |= source.get() << 16;
        results |= source.get() <<  8;
        results |= source.get();
        return results;
    }
    

    (Some error checking obviously needs to be added.)

    If many of the integers are actually small, you could try some
    variable length encoding, such as that used in Google Protocol
    Buffers. If most of your integers are in the range -64…63,
    this could result in a file only a quarter of the size (which
    again, will improve the time necessary to read and write).

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

Sidebar

Related Questions

Possible Duplicate: Writing to a XML file in Java I have below XML text
Possible Duplicate: Reading and Writing Configuration Files Okay, I have a config file, which
Possible Duplicate: Does free(ptr) where ptr is NULL corrupt memory? I'm writing a C
Possible Duplicate: Create new C++ object at specific memory address? I am writing what
Possible Duplicate: Why split the <script> tag when writing it with document.write()? I have
Possible Duplicate: C write in the middle of a binary file without overwriting any
Possible Duplicate: Workflow for statistical analysis and report writing I have been programming with
Possible Duplicate: Data not writing out to Database I'm trying to update a bit
Possible Duplicate: Writing a CSV file in .net Are there any good CSV writers
Possible Duplicate: Writing into a file objective c How do I create and write

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.