Skip to content

Instantly share code, notes, and snippets.

@belavina belavina/crc_benchmark.cpp Secret
Created Jan 9, 2018

What would you like to do?
#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.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)),
////////////////// 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]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.