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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:51:07+00:00 2026-05-16T00:51:07+00:00

The usual way to read a file in C++ is this one: std::ifstream file(file.txt,

  • 0

The usual way to read a file in C++ is this one:

std::ifstream file("file.txt", std::ios::binary | std::ios::ate);
std::vector<char> data(file.tellg());
file.seekg(0, std::ios::beg);
file.read(data.data(), data.size());

Reading a 1.6 MB file is almost instant.

But recently, I discovered std::istream_iterator and wanted to try it in order to code a beautiful one-line way to read the content of a file. Like this:

std::vector<char> data(std::istream_iterator<char>(std::ifstream("file.txt", std::ios::binary)), std::istream_iterator<char>());

The code is nice, but very slow. It takes about 2/3 seconds to read the same 1.6 MB file. I understand that it may not be the best way to read a file, but why is it so slow?

Reading a file in a classical way goes like this (I’m talking only about the read function):

  • the istream contains a filebuf which contains a block of data from the file
  • the read function calls sgetn from the filebuf, which copies the chars one by one (no memcpy) from the inside buffer to “data”‘s buffer
  • when the data inside of the filebuf is entirely read, the filebuf reads the next block from the file

When you read a file using istream_iterator, it goes like this:

  • the vector calls *iterator to get the next char (this simply reads a variable), adds it to the end and increases its own size
  • if the vector’s allocated space is full (which happens not so often), a relocation is performed
  • then it calls ++iterator which reads the next char from the stream (operator >> with a char parameter, which certainly just calls the filebuf’s sbumpc function)
  • finally it compares the iterator with the end iterator, which is done by comparing two pointers

I must admit that the second way is not very efficient, but it’s at least 200 times slower than the first way, how is that possible?

I thought that the performance killer was the relocations or the insert, but I tried creating an entire vector and calling std::copy, and it’s just as slow.

// also very slow:
std::vector<char> data2(1730608);
std::copy(std::istream_iterator<char>(std::ifstream("file.txt", std::ios::binary)), std::istream_iterator<char>(), data2.begin());
  • 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-16T00:51:08+00:00Added an answer on May 16, 2026 at 12:51 am

    You should compare apple-to-apple.

    Your first code read unformatted binary data because you use the function member “read”. And not because you use std::ios_binary by the way, see http://stdcxx.apache.org/doc/stdlibug/30-4.html for more explication, but in short : “The effect of the binary open mode is frequently misunderstood. It does not put the inserters and extractors into a binary mode, and hence suppress the formatting they usually perform. Binary input and output is done solely by basic_istream<>::read() and basic_ostream<>::write()”

    So your second code with istream_iterator read formatted text. It’s way slower.

    If you want to read unformatted binary data, use istreambuf_iterator :

    #include <fstream>
    #include <vector>
    #include <iterator>
    
    std::ifstream file( "file.txt", std::ios::binary);
    std::vector<char> buffer((std::istreambuf_iterator<char>(file)),
                              std::istreambuf_iterator<char>());   
    

    On my platform (VS2008), istream_iterator is about x100 slower than read(). istreambuf_iterator performs better, but still x10 slower than read().

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

Sidebar

Related Questions

The usual way to assign color box functionality on a link is like this:
This is the usual way for declare a Java array: int[] arr = new
This is my usual way to debug javascript. Include alert(0); to break the flow
What is the usual/clearest way to write this in Python? value, _ = func_returning_a_tuple()
As usual, I've Googled this and read through the documentation ( W3C's CSS Selectors
As usual, I have Googled this a fair bit and read up on the
I have a binary file that I would like to read with Fortran. The
What is the preferred/ usual way of storing data that is entered by the
The usual way to resolve lnk involve using WShell.WshShortcut or IShellLink that way :
A usual way to put, for example a menu tab with background is as

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.