XOR two files together I am trying to XOR two files each 1Mb which contains only 0 and 1. It works, but I think it is very slow for the C++ program - approximately 4 sec.

Could someone, please, suggest the fastest XOR method on large files or at least tell what you think about XORing 1MB files running time? May 0.5sec or 1sec? or even less... 
What should be my goal related to time?

PS
Yes, I did not read files by chunks or mapping them.



#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;
#include &lt;cstring&gt;
#include &lt;algorithm&gt;
#include &lt;iterator&gt;
using namespace std;

inline std::vector&lt;int&gt; read_vector_from_disk(std::string file_path)
{
    std::ifstream instream(file_path, std::ios::in | std::ios::binary);
    std::vector&lt;int&gt; data((std::istreambuf_iterator&lt;char&gt;(instream)), std::istreambuf_iterator&lt;char&gt;());
    return data;
}

int main()
{
    // Open result file.
    fstream new_file;
    new_file.open("output.txt", ios::out | ios::app);
    if (!new_file)
    {
        cout &lt;&lt; "File creation failed";
    }

    // Here we will store the result
    std::vector&lt;int&gt; xored_file;

    std::vector&lt;int&gt; in_data = read_vector_from_disk("first_arg.txt");
    std::vector&lt;int&gt; out_data = read_vector_from_disk("second_arg.txt");

    //creating iterator
    vector&lt;int&gt;::iterator first_iter = in_data.begin();
    vector&lt;int&gt;::iterator second_iter = out_data.begin();

    //printing all elements
    cout &lt;&lt; "XOR-ed elements are: ";


    for (; first_iter != in_data.end() &amp;&amp; second_iter != out_data.end(); first_iter++, second_iter++)
    {
        int xored = *first_iter ^ *second_iter;
        xored_file.push_back(xored);
    }

    // Print result.
    vector&lt;int&gt;::iterator out_iter = xored_file.begin();
    for (; out_iter != xored_file.end(); out_iter++)
    {
        new_file &lt;&lt; *out_iter;
    }


    cout &lt;&lt; "Complete";

    return 0;
}
#include &lt;cstring&gt;



seems unused.


using namespace std;



Not a good plan - just use the std:: qualifier, or import just the names you need, in the smallest reasonable scope.


inline std::vector&lt;int&gt; read_vector_from_disk(std::string file_path)
{
    std::ifstream instream(file_path, std::ios::in | std::ios::binary);
    std::vector&lt;int&gt; data((std::istreambuf_iterator&lt;char&gt;(instream)), std::istreambuf_iterator&lt;char&gt;());
    return data;
}



This is a very inefficient way to copy a file into memory - the vector will likely have to reallocate (copying its contents) several times, as the input iterators can't be used to predict the eventual size.

Two faster approaches would be


memory-map the file contents, probably using Boost::interprocess
work in a streaming fashion with character-by-character input (e.g. using std::transform())


Also, there's no check whether any of this reading succeeded at all.  That's bad.


std::vector&lt;int&gt; xored_file;



Again, we have a vector we'll append to without first reserving capacity.  That's reducing your efficiency.  I don't see why we need to store a copy of output, instead of immediately writing it to the output stream.

When we've finished writing, we should close the file and confirm that it was successfully written:

new_file.close();
if (!new_file) {
    std::cerr &lt;&lt; "Failed to write output file\n";
    return EXIT_FAILURE;
}