Fastest and Efficient way of file read and write

What is the fastest way to read and write file in C++ ?

Data is plain text (human readable) not binary, with some control characters.
Last edited on
A call to OS-function that creates a copy of the file with the new name at the filesystem level?


The "real" answer depends on what are you reading from the file and what you have to do with it.
What is the fastest way to read and write file in C++ ?
Certainly using the system functions.

If using streams: setting the mode to binary might increase the speed since no conversion (regarding end of line) is done.
What is the fastest way to read and write file in C++ ?

You don't indicate the type of data. It's difficult to answer your question without knowing the type of data.

If it's character oriented data (\n delimited), use istream and ostream, use >> or getline and << as appropriate
If it's fixed size record oriented data or binary data, use istream::read and ostream::write or operating system calls.

If speed is really important and the file is in binary mode then forget the streams and use the good old stdio files - they are much faster.
Data is plain text (human readable) not binary, with some control characters.
Sorry I was not clear enough. Even when the data is plain text you can read and write it in binary format. Since you asked about the fastest way you should prefer the binary format. Here is a little speed comparison:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <stdio.h>
#include <time.h>

class StopWatch
{
public:
  void start ();
  void stop ();
  void reset ();
  double seconds ();
  unsigned int milli_seconds ();
private:
  time_t m_Start,
    m_Stop;
};

void StopWatch::start ()
{
  m_Start = clock ();
}

void StopWatch::stop ()
{
  m_Stop = clock ();
}

void StopWatch::reset ()
{
  m_Start = m_Stop = 0;
}

double StopWatch::seconds ()
{
  return double (m_Stop - m_Start) / CLOCKS_PER_SEC;
}

unsigned int StopWatch::milli_seconds ()
{
  return (double (m_Stop - m_Start) / CLOCKS_PER_SEC) * 1000;
}

typedef std::vector<std::string> StringVector;

void writeText (StringVector& vec);
void writeBinary (StringVector& vec);

int main ()
{
  StringVector v (1000000, "Hello ");
  StopWatch sw;

  sw.start ();
  writeText (v);
  sw.stop ();
  std::cout << "\n\nTime for writing text file: " << sw.milli_seconds () << " ms";

  sw.start ();
  writeBinary (v);
  sw.stop ();
  std::cout << "\n\nTime for writing binary file: " << sw.milli_seconds () << " ms";
  std::cout << "\n\n";
  system ("pause");
  return 0;
}

void writeText (StringVector& vec)
{
  FILE* dest = fopen ("TextFile.txt", "wt");
  if (dest == 0)
  {
    perror ("writeText ");
    return;
  }
  for (const std::string& s : vec)
  {
    fprintf (dest, "%s", s.c_str ());
  }
  fclose (dest);
}

void writeBinary (StringVector& vec)
{
  FILE* dest = fopen ("TextFile.bin", "wb");
  if (dest == 0)
  {
    perror ("writeBinary ");
    return;
  }
  for (const std::string& s : vec)
  {
    fwrite (s.c_str (), strlen (s.c_str ()), 1, dest);
  }
  fclose (dest);
}


Output:
1
2
3
Time for writing text file: 733 ms

Time for writing binary file: 546 ms
Controlling the size of your writes will help a lot more than you might think.

In other words, your best throughput will be to fill a memory buffer and write full and large blocks of data.

Your device probably supports 4k writes as the smallest size. Every time you write a file you have overhead which will impact performance. So the larger sized files you can write means less overhead, hopefully better performance.
Topic archived. No new replies allowed.