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 <iostream>
#include <fstream>
#include <vector>
#include <cstring>
#include <algorithm>
#include <iterator>
using namespace std;
inline std::vector<int> read_vector_from_disk(std::string file_path)
{
std::ifstream instream(file_path, std::ios::in | std::ios::binary);
std::vector<int> data((std::istreambuf_iterator<char>(instream)), std::istreambuf_iterator<char>());
return data;
}
int main()
{
// Open result file.
fstream new_file;
new_file.open("output.txt", ios::out | ios::app);
if (!new_file)
{
cout << "File creation failed";
}
// Here we will store the result
std::vector<int> xored_file;
std::vector<int> in_data = read_vector_from_disk("first_arg.txt");
std::vector<int> out_data = read_vector_from_disk("second_arg.txt");
//creating iterator
vector<int>::iterator first_iter = in_data.begin();
vector<int>::iterator second_iter = out_data.begin();
//printing all elements
cout << "XOR-ed elements are: ";
for (; first_iter != in_data.end() && second_iter != out_data.end(); first_iter++, second_iter++)
{
int xored = *first_iter ^ *second_iter;
xored_file.push_back(xored);
}
// Print result.
vector<int>::iterator out_iter = xored_file.begin();
for (; out_iter != xored_file.end(); out_iter++)
{
new_file << *out_iter;
}
cout << "Complete";
return 0;
}
0
and1
, or'0'
and'1'
? \$\endgroup\$