Skip to content

Instantly share code, notes, and snippets.

@belavina
Created January 8, 2018 20:14
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/497c60e4c905c35747c3fe964a14e2c9 to your computer and use it in GitHub Desktop.
Save belavina/497c60e4c905c35747c3fe964a14e2c9 to your computer and use it in GitHub Desktop.
/**********************************************************************
*
* Filename: main.c
*
* Description: A simple test program for the CRC implementations.
*
* Notes: To test a different CRC standard, modify crc.h.
*
*
* Copyright (c) 2000 by Michael Barr. This software is placed into
* the public domain and may be used for any purpose. However, this
* notice must not be changed or removed and no warranty is either
* expressed or implied by its publication or distribution.
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include "crc.h"
#include <iostream>
#include <algorithm>
#include <numeric>
#include <fstream>
#include <streambuf>
#include <cstring>
#include <string>
#include <chrono>
#include <math.h>
#define NUM_CRC 3000
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 - check //////////////////
unsigned char test[] = "123456789";
int size =9;
printf("The check value for the %s standard is 0x%X\n", CRC_NAME, CHECK_VALUE);
// populate lookup table
crcInit();
printf("The crcFast() of \"123456789\" is 0x%X\n", crcFast(test, 9));
printf("The crcFastAlt() of \"123456789\" is 0x%X\n", crcFastAlt(test, 9));
////////////////// CRC - benchmark //////////////////
uint32_t* checksums = (uint32_t*) malloc(NUM_CRC * sizeof(uint32_t));
uint32_t* checksumsf = (uint32_t*) malloc(NUM_CRC * sizeof(uint32_t));
long* ms = (long*) malloc(NUM_CRC * sizeof(long));
long* msf = (long*) malloc(NUM_CRC * sizeof(long));
ch::steady_clock::time_point ts, te;
ch::steady_clock::time_point ts1, te1;
printf("Started calculating CRCs \n");
for (int i=0; i<NUM_CRC; i++) {
ts = ch::steady_clock::now();
auto checksum = crcFastAlt((const unsigned char*)str.data(), str.length());
checksums[i] = checksum;
te = ch::steady_clock::now();
ms[i] = (ch::duration_cast<ch::microseconds>(te - ts)).count();
ts1 = ch::steady_clock::now();
checksum = crcFast((const unsigned char*)str.data(), str.length());
checksumsf[i] = checksum;
te1 = ch::steady_clock::now();
msf[i] = (ch::duration_cast<ch::microseconds>(te1 - ts1)).count();
// shuffle strings
std::random_shuffle(str.begin(), str.end());
}
printf("Finished calculating CRCs \n");
printf("Time taken on Alternative CRC: \n");
reportTime(std::accumulate(ms, ms+NUM_CRC,0));
printf("Time taken on Original CRC: \n");
reportTime(std::accumulate(msf, msf+NUM_CRC,0));
bool clean = true;
for (int i=0; i<NUM_CRC; i++) {
if(checksums[i] != checksumsf[i]){
printf("Crc #%d : %u =/= %u \n",i, checksums[i], checksumsf[i]);
clean = false;
}
}
if(!clean) {
printf("Some CRC(s) do not match! \n");
}
free(checksums);
free(checksumsf);
free(ms);
free(msf);
return 0;
} /* main() */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment