본문 바로가기
프로그래밍/C & C++

boost를 이용한 crc 계산

by 체리 2022. 1. 31.
반응형

ubuntu에 boost library 설치하기

 sudo apt install libboost-all-dev

 

#include <iostream>
#include <chrono>
#include <cstdio>
#include <boost/crc.hpp>

using namespace std;
using boost::crc_32_type;

int main()
{
    char testStr1[] = "12345678901234567890";
    char testStr2[] = "12345678901234567891";
    crc_32_type crc;

    printf("crc size: %d\n", sizeof(unsigned int));
    crc.process_bytes(testStr1, sizeof(testStr1));
    printf("testStr1 crc: %08x\n", crc.checksum());
    crc.process_bytes(testStr2, sizeof(testStr2));
    printf("testStr2 crc: %08x\n", crc.checksum());

    const size_t BYTE_4K = 4096;
    auto testBuf = new char[BYTE_4K];

    auto start = chrono::system_clock::now();
    crc.process_bytes(testBuf, BYTE_4K);
    auto result = crc.checksum();
    auto end = chrono::system_clock::now() - start;

    printf("testBuf crc: %08x\n", result);
    printf("elapsed time: %ld us\n", chrono::duration_cast<chrono::microseconds>(end).count());

    delete testBuf;

    return 0;
}

 

실행하면..

 crc size: 4
 testStr1 crc: 81211fa4
 testStr2 crc: 03beef0d
 testBuf crc: fa5e66f4
 elapsed time: 12 us

return value type은 unsigned int

4k buffer를 crc 계산하는데 내 pc에서 12 us 걸림 (amd 5600x)

반응형

댓글0