{"slug": "how-to-optimize-io-in-c", "title": "How to optimize IO in C++", "summary": "A developer explains how to optimize I/O in C++ by disabling synchronization between C++ streams and C standard streams, and by manually flushing the output buffer. The post details the performance costs of reading and writing data due to operating system calls and context switching, and provides code examples for competitive programming.", "body_md": "[Link to the repository containing all examples](https://github.com/MiztonCodes/OptimizeIO)\n\nBack when I was at my first class of Data Structures and Algorithms, I started to solve competitive programming questions in judges like CodeForces, I didn't know why my code was slower if my solution was efficient (at least in theory), my professor explain to the class the reason why our programs were slow, because of I/O is an expensive task for computers.\n\nTo start working with the examples in this article just clone the repository and play with it as long the article explains how to run the code.\n\n```\ngit clone https://github.com/MiztonCodes/OptimizeIO.git\n```\n\n`std::cout`\n\nand `std::cin`\n\nare slow?\nIn C++ by default, both `std::cin`\n\nand `std::cout`\n\nstreams are synchronized with standard C `scanf`\n\nand `printf`\n\nstreams and at the same time every single call to `std::cin`\n\nflushes the `std::cout`\n\nbuffer, because `std::cin`\n\nbefore reading input, wants to output any pending prompt to the user, which can cause performance issues because reading and writing are expensive tasks due to the need to make calls to the operating system, the solution is to disable all of this synchronizations and manually flushing the output buffer whenever it is needed.\n\n*Note: For convenience in all examples I'll use #include<bits/stdc++.h> header to simplify the imports, this header includes everything we'll need and this header only works on GCC compiler.*\n\nWhen you run your program, it is like an isolated box inside your computer, ready to do some work, the **input** process involves moving raw data from an external source like a keyboard, a file or even the network to your program *(reading)*, the **output** process involves moving data from your program to an external destination like a monitor, the console, a file, the network or even an external device *(writing)*, for both processes your program does not read or write directly to the **input** and **output** sources, because each source has its own way to transfer data, C++ provides a uniform interface to hide that complexity through the usage of **streams**.\n\nIn C++ and other programming languages, a **stream** is an abstraction layer that hides the complexity of dealing with external sources to *read* and *write* data from your program, so you can use a unified interface with the following operators for writing `dest << data`\n\nand reading `source >> dest`\n\n. The **stream** is able to translate the raw input data into data types that your program can understand like `int`\n\n, `bool`\n\n, `char`\n\n, so basically the **stream** is only responsible to transport data.\n\nA **buffer** is a contiguous block of memory located at the RAM, reserved to *batch* data to reduce the amount of calls to the operating system, once the **buffer** has reached its full capacity the operating system performs one single efficient transfer of **buffer's** data to its final destination, so the memory of the **buffer** can be reused and then wait until the **buffer** reached its full capacity again, this makes **buffers** the perfect place to hold data during your program execution.\n\nAs in real life the travel from shorter distances almost always implies shorter travel times and plus the planning time, reading and writing data is expensive because the data must travel from one location to its destination and at same time the CPU needs to manage the transfer of data.\n\nReading and writing data implies much more than just moving data from one location to its destination, when a reading or writing operation is needed a call to the operating system is made and the process first makes the CPU to switch the context of whatever it was doing before, then verifies permission and perform operations on memory to finally move the data, while all this happens the CPU spends a lot time and operations waiting to move the data to its destination.\n\nThis is due to the C++ history, C++ at first was C with classes, nowadays to keep compatibility and consistency it keeps synchronized standard input and output streams, so you can mix both styles in a single program and expects the output order be preserved when mixing both styles, but it introduces performance issues, especially when you don't need that synchronization like in competitive programming or newer projects where compatibility with C is not needed.\n\nAs I said earlier, just disable all synchronizations and manually flush the output stream when needed, just add the following lines to the beginning of your `main`\n\nfunction.\n\n**Warning: When you disable synchronization, don't mix both styles because it can produce an undefined behavior**\n\n```\n// Disable synchronization with standard C scanf and printf streams\nstd::ios_base::sync_with_stdio(false); \n// Disable synchronization between std::cin and std::cout streams\nstd::cin.tie(nullptr);\n```\n\n*Note: You only need to untie std::cin like in the code snippet above because std::cout is not tied to anything, so just remember to only untie std::cin.*\n\nTo demonstrate performance gains, you can run the following tests to verify it by yourself.\n\n*Note: To demonstrate a noticeable performance gain, use as input the dataset.txt file hosted in the repository of this example, this file has a size of 75 MB and contains 10 million integer numbers from 1 to 10'000'000 this is a really simple example to demonstrate performance gains even when your program only read data and don't perform any other computation.*\n\nTo compile and run the next examples just use the following commands.\n\n```\n# Move the section directory\ncd 01_Optimize_Writing\n\n# Give execution permissions to compilation scripts\nchmod +x run_without_optimization.sh\nchmod +x run_with_optimization.sh\n\n# Run the examples below\n./run_without_optimization.sh\n./run_with_optimization.sh\n#include <bits/stdc++.h>\n\nint main() {\n  // Get the current time before executing the heavy part.\n  auto start = std::chrono::high_resolution_clock::now();\n\n    // Heavy part reading data.\n  int val = 0;\n  for (long long i = 0; i < 10'000'000; ++i) {\n    std::cin >> val;\n  }\n\n  // Get the current time after executing the heavy part.\n  auto end = std::chrono::high_resolution_clock::now();\n\n  // Calculate the exeuction time lapse.\n  std::chrono::duration<double> diff = end - start;\n\n  // Using std::cerr to print diagnostic data immediately.\n  std::cerr << \"Time taken: \" << diff.count() << \" seconds\" << std::endl;\n}\n```\n\nYou will get an output like this\n\n```\nTime taken: 5.39379 seconds\n#include <bits/stdc++.h>\n\nint main() {\n  // Optimization\n  std::ios_base::sync_with_stdio(false);\n  std::cin.tie(nullptr);\n\n  // Get the current time before executing the heavy part.\n  auto start = std::chrono::high_resolution_clock::now();\n\n    // Heavy part reading data.\n  int val = 0;\n  for (long long i = 0; i < 10'000'000; ++i) {\n    std::cin >> val;\n  }\n\n  // Get the current time after executing the heavy part.\n  auto end = std::chrono::high_resolution_clock::now();\n\n  // Calculate the exeuction time lapse.\n  std::chrono::duration<double> diff = end - start;\n\n  // Using std::cerr to print diagnostic data immediately.\n  std::cerr << \"Time taken: \" << diff.count() << \" seconds\" << std::endl;\n}\n```\n\nYou will get an output like this\n\n```\nTime taken: 0.667611 seconds\n```\n\nEven in this really simple example we can observe a performance gain of five times faster execution, just disabling the synchronization of streams.\n\nAvoid the usage of `std::endl`\n\nto add a line break , because `std::endl`\n\nadd a line break and at the same time it flushes the buffer (which is an expensive operation), so if you only want to add a line break just use the `\\n`\n\ncharacter like in the next example:\n\n```\n/* \n    Avoid this style to add line breaks,\n    it will add a line break at the end of the string\n    and flush the output buffer which will cause\n    performance issue.\n*/\nstd::cout << \"Woody!\" << std::endl;\n\n/*\n    Prefer this style to add line breaks,\n    it will add a line break at the end of the string\n    and will not flush the output, so the string is going\n    to be batched at the output buffer,\n*/\nstd::cout << \"Woody!\\n\";\n```\n\nTo demonstrate performance gains, you can run the following tests to verify it by yourself.\n\nTo compile and run the next examples just use the following commands.\n\n```\n# Move the section directory\ncd 01_Optimize_Reading\n\n# Give execution permissions to compilation scripts\nchmod +x run_without_optimization.sh\nchmod +x run_with_optimization.sh\n\n# Run the examples below\n./run_without_optimization.sh\n./run_with_optimization.sh\n```\n\n`std::endl`\n\n```\n#include <bits/stdc++.h>\n\nint main() {\n  /*\n      In this case the following lines does not affect in any\n      case because we are not reading data.\n  */\n  std::ios_base::sync_with_stdio(false);\n  std::cin.tie(nullptr);\n\n  // Get the current time before executing the heavy part.\n  auto start = std::chrono::high_resolution_clock::now();\n\n  // Heavy part writing data.\n  for (int i = 1; i <= 10'000'000; ++i) {\n    std::cout << \"Iteration \" << i << std::endl;\n  }\n\n  // Get the current time after executing the heavy part.\n  auto end = std::chrono::high_resolution_clock::now();\n\n  // Calculate the exeuction time lapse.\n  std::chrono::duration<double> diff = end - start;\n\n  // Using std::cerr to print diagnostic data immediately.\n  std::cerr << \"Time taken: \" << diff.count() << \" seconds\" << std::endl;\n}\n```\n\nYou will get an output like this\n\n```\nTime taken: 5.76418 seconds\n```\n\n`\\n`\n\n```\n#include <bits/stdc++.h>\n\nint main() {\n  /*\n      In this case the following lines does not affect in any\n      case because we are not reading data.\n  */\n  std::ios_base::sync_with_stdio(false);\n  std::cin.tie(nullptr);\n\n  // Get the current time before executing the heavy part.\n  auto start = std::chrono::high_resolution_clock::now();\n\n  // Heavy part writing data.\n  for (int i = 1; i <= 10'000'000; ++i) {\n    std::cout << \"Iteration \" << i << \"\\n\";\n  }\n\n  // Get the current time after executing the heavy part.\n  auto end = std::chrono::high_resolution_clock::now();\n\n  // Calculate the exeuction time lapse.\n  std::chrono::duration<double> diff = end - start;\n\n  // Using std::cerr to print diagnostic data immediately.\n  std::cerr << \"Time taken: \" << diff.count() << \" seconds\" << std::endl;\n}\n```\n\nYou will get an output like this\n\n```\nTime taken: 1.47316 seconds\n```\n\nEven in this really simple example we can observe a performance gain of five times faster execution, just by not flushing the output buffer every single time we need to add a line break.\n\nTo compile and run the next examples just use the following commands.\n\n```\n# Move the section directory\ncd 02_Miscellaneous\n\n# Give execution permissions to compilation scripts\nchmod +x run_error_log.sh\nchmod +x run_format_output.sh\nchmod +x run_redirect_io.sh\nchmod +x run_operator_overloading.sh\n\n# Run the examples below\n./run_error_log.sh\n./run_format_output.sh\n./run_redirect_io.sh\n./run_operator_overloading.sh\n```\n\n`std::cerr`\n\nstream for errors and diagnostics\nIn addition to the standard `std::cin`\n\nreading and `std::cout`\n\nwriting streams, C++ provides another `std::cerr`\n\nstream to write errors and diagnostic data, the difference between `std::cout`\n\nand `std::cerr`\n\nis mainly the way each of them outputs data, while `std::cout`\n\nholds data in a buffer, `std::cerr`\n\nis unbuffered and outputs data immediately after a call to it, so if you use `std::cout`\n\nand the program execution is interrupted, the output buffer is going to be destroyed along the program and data stored in the buffer will be lost.\n\n```\n#include <bits/stdc++.h>\n\nint main() {\n  std::cout << \"First std::cout call\\n\";\n  std::cerr << \"Using this to print error messages and diagnostic data.\\n\";\n  std::cout << \"Second std::cout call\\n\";\n}\n```\n\nYou will get this output\n\n```\nFirst std::cout call\nUsing this to print error messages and diagnostic data.\nSecond std::cout call\n```\n\nAs you can read a call to `std::cerr`\n\nwrites immediately to the console because it is unbuffered.\n\nIf you are using C++20 or higher, you can use the modern `std::format`\n\nto print in fixed-point, and scientific notations like in the following example.\n\n```\n#include <bits/stdc++.h>\n\nint main() {\n  double my_number = 7.2069;\n\n  // Default formatting\n  std::cout << std::format(\"Default notation: {}\\n\", my_number);\n\n  // Fixed-point and rounded up with 2 decimal places\n  std::cout << std::format(\"Fixed and rounded up (2 decimal): {:.2f}\\n\", my_number);\n\n  // Fixed-point and floored with 2 decimal places\n  std::cout << std::format(\"Fixed and floored (2 decimal): {:.2f}\\n\", std::floor(my_number));\n\n  // Fixed-pint and ceiled with 2 decimal places\n  std::cout << std::format(\"Fixed and ceiled (2 decimal): {:.2f}\\n\", std::ceil(my_number));\n\n  // Scientific notation\n  std::cout << std::format(\"Scientific and rounded up: {:.2e}\\n\", my_number);\n\n  return 0;\n}\n```\n\nYou will get an output like this\n\n```\nDefault notation: 7.2069\nFixed and rounded up (2 decimal): 7.21\nFixed and floored (2 decimal): 7.00\nFixed and ceiled (2 decimal): 8.00\nScientific and rounded up: 7.21e+00\n```\n\nThis is useful when you need to deal with decimal places and rounding decimal numbers based on the question requirements.\n\nIt is very tedious to read and write data directly from the terminal, and have mixed output along with logs, errors, diagnostics and output data, so you can redirect all of this input and output categories to files in your filesystem when you execute the program.\n\nThe operating system has multiple channels to read and write data, so when you run your program you can redirect channels to files like in the following example.\n\n```\n./bin 0< input.txt 1> output.txt 2> errors.txt\n```\n\nAnd you can still use `std::cin`\n\n, `std::cout`\n\nand `std::cerr`\n\njust like any other program and have a more cleaner workspace, this is helpful if you have a really large `input.txt`\n\ndataset file and copy pasting to the terminal is not an option.\n\n*Extra: There is also an standard std::log stream that is buffered and writes data to the error channel, use it only when you need to write some extra information but you don't need it immediately.*\n\nIn C++ you can keep using `std::cin`\n\nand `std::cout`\n\nbut redirect the streams to read and write from a custom string or a file streams and then rollback the original ones to read and write from the standard input and output.\n\nIn the following example a **RAII** *(Resource Acquisition Is Initialization)* pattern is implemented to ensure the swap and rollback of buffers is secure even if an exception happens or the program ends early.\n\n*Note: You can run this example using the files provided at the repository of the article.*\n\n```\n#include <bits/stdc++.h>\n\n/*\n  This class will hold the overall logic to\n  swap buffers given a stream and a new buffer\n  using the RAII pattern.\n*/\nclass stream_redir {\nprivate:\n  // Holds the stream\n  std::ios& _stream;\n  // Holds the original buffer of the stream\n  std::streambuf* _origin_buff;\npublic:\n  /*\n    The constructor will save a reference to the stream\n    and will hold a pointer to the original buffer while\n    assigning the new buffer to the stream.\n  */\n  stream_redir(\n    std::ios& stream,\n    std::streambuf* new_buffer\n  ): _stream(stream), \n    _origin_buff(stream.rdbuf(new_buffer)) {}\n  // The destructor automatically restores the original buffer.\n  ~stream_redir() {\n    _stream.rdbuf(_origin_buff);\n  }\n  // Prevent copying to avoid multiple restorations of the same buffer.\n  stream_redir(const stream_redir&) = delete;\n  stream_redir operator=(const stream_redir&) = delete;\n};\n\nvoid redirect_output_to_string() {\n  std::cout << \"=== REDIRECT OUTPUT TO A STRING ===\\n\";\n  std::stringstream custom_out;\n  std::cout << \"Before redirection\\n\";\n  // In this scope the redirection will happens.\n  {\n    stream_redir redir(std::cout, custom_out.rdbuf());\n    std::cout << \"This message is going to be stored at custom_out buffer\";\n  } // At the end of the scope, buffer restoration happens.\n  std::cout << \"After redirection\\n\";\n  std::cout << \"custom_out contents:\\n\";\n  std::cout << custom_out.rdbuf() << \"\\n\";\n}\n\nvoid redirect_input_to_string() {\n  std::cout << \"=== REDIRECT INPUT TO A STRING ===\\n\";\n  std::string input_data = \"I'm happy learning C++\";\n  std::stringstream custom_in(input_data);\n  std::string read_data;\n  read_data.reserve(input_data.size());\n  // In this scope the redirection will happens.\n  {\n    stream_redir redir(std::cin, custom_in.rdbuf());\n    while (std::getline(std::cin, read_data)) {}\n  } // At the end of the scope, buffer restoration happens.\n  std::cout << \"read_data contents:\\n\";\n  std::cout << read_data << \"\\n\";\n}\n\nvoid redirect_output_to_file() {\n  std::cout << \"=== REDIRECT OUTPUT TO A FILE ===\\n\";\n  std::filebuf custom_out;\n  custom_out.open(\"dataset_temp.txt\", std::ios::out);\n  std::vector<int> nums{1, 2, 3, 4, 5};\n  std::cout << \"Before redirection\\n\";\n  // In this scope the redirection will happens.\n  {\n    stream_redir redir(std::cout, &custom_out);\n    for (const int& n : nums) {\n      std::cout << n << \"\\n\";\n    }\n  } // At the end of the scope, buffer restoration happens.\n  std::cout << \"After redirection\\n\";\n}\n\nvoid redirect_input_to_file() {\n  std::cout << \"=== REDIRECT INPUT TO A FILE ===\\n\";\n  std::filebuf custom_in;\n  // Here read data from the file created by redirect_output_to_file function\n  custom_in.open(\"dataset_temp.txt\", std::ios::in);\n  constexpr size_t MAX_NUM_SIZE = 5;\n  std::vector<int> nums;\n  nums.reserve(MAX_NUM_SIZE);\n  std::cout << \"Before reading nums.size() == \" << nums.size() << \"\\n\";\n  // In this scope the redirection will happens.\n  {\n    stream_redir redir(std::cin, &custom_in);\n    for (size_t i = 0; i < MAX_NUM_SIZE; ++i) {\n      int n = 0;\n      std::cin >> n;\n      nums.push_back(n);\n    }\n  } // At the end of the scope, buffer restoration happens.\n  std::cout << \"After reading nums.size() == \" << nums.size() << \"\\n\";\n  for (const auto& n : nums) std::cout << n << \" \";\n  std::cout << \"\\n\";\n}\n\nint main() {\n  // Optimization for reading and writing\n  std::ios_base::sync_with_stdio(false);\n  std::cin.tie(nullptr);\n  // Examples\n  redirect_output_to_string();\n  redirect_input_to_string();\n  redirect_output_to_file();\n  redirect_input_to_file();\n}\n```\n\nYou will get an output like this\n\n```\n=== REDIRECT OUTPUT TO A STRING ===\nBefore redirection\nAfter redirection\ncustom_out contents:\nThis message is going to be stored at custom_out buffer\n=== REDIRECT INPUT TO A STRING ===\nread_data contents:\nI'm happy learning C++\n=== REDIRECT OUTPUT TO A FILE ===\nBefore redirection\nAfter redirection\n=== REDIRECT INPUT TO A FILE ===\nBefore reading nums.size() == 0\nAfter reading nums.size() == 5\n1 2 3 4 5\n```\n\nSo as you can see, the streams can be redirected to read and write data to other sources, this is useful for testing when you try to mock certain input or output sources while keep using the standard input and output streams.\n\nSometimes you will need to read and write data based on a format or structure, for example to read data and create an instance of a class or struct, maybe you want to print all your vector public members to a certain format so other programs can understand your data.\n\nIn C++ you can overload the `<<`\n\noutput operator and the `>>`\n\ninput operator to work with complex data types like in the next example.\n\n```\n#include <bits/stdc++.h>\n\nnamespace mizton {\n  struct vector_3d {\n    int x, y, z;\n  };\n};\n\n/*\n  Overloading of output operator on output streams.\n*/\nstd::ostream& operator<<(std::ostream& os, const mizton::vector_3d& v) {\n  os << \"vector{\" << v.x << \",\" << v.y << \",\" << v.z << \"}\\n\";\n  return os;\n}\n\n/*\n  Overloading of input operator on input streams.\n*/\nstd::istream& operator>>(std::istream& is, mizton::vector_3d& v) {\n  int x = 0, y = 0, z = 0;\n\n  /*\n    In this case we don't allow negative numbers\n    as coordinate values, so we will set the read operation\n    as failed, so the calling code can handle the error.\n  */\n  if (is >> x >> y >> z) {\n    if (x < 0 || y < 0 || z < 0) {\n      is.setstate(std::ios::failbit); // Indicates input conversion fails\n      std::cerr << \"Negative coordinate value can't be converted to a mizton::vector_3d structure\\n\";\n    } else {\n      v.x = x;\n      v.y = y;\n      v.z = z;\n    }\n  } else {\n    is.setstate(std::ios::badbit); // Indicates a loss of integrity\n    std::cerr << \"Lost data while trying to read a mizton::vector_3d structure\\n\";\n  }\n\n  return is;\n}\n\nint main() {\n  // Mock input stream\n  std::stringstream input_stream(R\"(\n    3\n    1 0 1\n    1 0 3\n    5 6 1\n  )\");\n  // Reading from input_stream\n  int vector_size = 0;\n  input_stream >> vector_size;\n  assert(vector_size >= 0); // Ensure the first number read is positive\n  std::vector<mizton::vector_3d> v_list(vector_size);\n  for (int i = 0; i < vector_size; ++i) {\n    /*\n      If input reading fails, just ends the program\n      normally performing a clean up of variables and\n      calling destructors and return exit code of a failure.\n    */\n    if (!(input_stream >> v_list[i])) {\n      return EXIT_FAILURE;\n    }\n  }\n  // Writing data to standard output\n  for (const auto& v : v_list) {\n    std::cout << v;\n  }\n  return EXIT_SUCCESS;\n}\n```\n\nAfter running the code above, you will get an output like this\n\n```\nvector{1,0,1}\nvector{1,0,3}\nvector{5,6,1}\n```\n\nIf you change some of the values for vectors to negative, the program will ends and an error like this will be out\n\n```\nNegative coordinate value can't be converted to a mizton::vector_3d structure\n```\n\nIn real life, using streams and buffers in C++ can be really complex and allow us to create really performant systems, I have shared what I found useful to deal with, especially when I was working with online judges like **CodeForces** to test my solution.", "url": "https://wpnews.pro/news/how-to-optimize-io-in-c", "canonical_source": "https://dev.to/mizton/how-to-optimize-io-in-c-2a0e", "published_at": "2026-06-26 18:18:37+00:00", "updated_at": "2026-06-26 19:04:35.369051+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["CodeForces", "GCC"], "alternates": {"html": "https://wpnews.pro/news/how-to-optimize-io-in-c", "markdown": "https://wpnews.pro/news/how-to-optimize-io-in-c.md", "text": "https://wpnews.pro/news/how-to-optimize-io-in-c.txt", "jsonld": "https://wpnews.pro/news/how-to-optimize-io-in-c.jsonld"}}