Skip to content

Instantly share code, notes, and snippets.

@belavina
Created January 9, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save belavina/f060e3715cb2a8f5bf26ff9f66cc2c1f to your computer and use it in GitHub Desktop.
Save belavina/f060e3715cb2a8f5bf26ff9f66cc2c1f to your computer and use it in GitHub Desktop.
#include <boost/crc_opt.hpp>
#include <iostream>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <streambuf>
#include <cstring>
#include <string>
#include <chrono>
#include <math.h>
#define NUM_CRC 3000
int GetCrc32(const std::string& my_string) {
boost::crc_32_type result;
result.process_bytes(my_string.data(), my_string.length());
return result.checksum();
}
void reportTime(long sum_ms) {
printf("-| Average time taken is : %f ms\n", (sum_ms/NUM_CRC)*0.001);
printf("-| Total time spent on CRC : %f ms\n", sum_ms*0.001);
}
int main(int argc, char** argv)
{
namespace ch = std::chrono;
bool printChecksums = false;
if(argc < 2) {
std::cerr << "Need Filename as argument!" << std::endl;
}
if (argc > 2) { printChecksums = std::string(argv[2]) == "checksums"; }
// read file
std::ifstream f(argv[1]);
std::string str((std::istreambuf_iterator<char>(f)),
std::istreambuf_iterator<char>());
////////////////// CRC //////////////////
int* checksums = (int*) malloc(NUM_CRC * sizeof(int));
long* ms = (long*) malloc(NUM_CRC * sizeof(long));
ch::steady_clock::time_point ts, te;
printf("Started calculating CRCs \n");
for (int i=0; i<NUM_CRC; i++) {
ts = ch::steady_clock::now();
checksums[i] = GetCrc32(str);
te = ch::steady_clock::now();
ms[i] = (ch::duration_cast<ch::microseconds>(te - ts)).count();
// shuffle strings
std::random_shuffle(str.begin(), str.end());
}
printf("Finished calculating CRCs \n");
reportTime(std::accumulate(ms, ms+NUM_CRC,0));
if (printChecksums) {
for (int i=0; i<NUM_CRC; i++) {
printf("Crc #%d = %d\n",i, checksums[i]);
}
}
free(checksums);
free(ms);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment