Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > C/C++: Общие вопросы > тормоза boost::crc_32_type


Автор: boostcoder 3.9.2010, 06:05
всем доброго утра.

накатал такой промер для сравнения скорости подсчета:
Код


#include <boost/cstdint.hpp>
#include <boost/array.hpp>
#include <boost/progress.hpp>
#include <boost/crc.hpp>
#include <fstream>
#include <iostream>
#include <zlib.h>

/***************************************************************************/

int main(int argc, char** argv) {
   const boost::uint32_t bsize = 1024*32;
   boost::array<char, bsize> buff;
   const char* fname = "файло.бин";

   for ( int idx = 0; idx < 1024; ++idx ) {
      {  boost::progress_timer timer;
         unsigned long adler = 0;
         std::ifstream file(fname, std::ios::binary);
         while ( file ) {
            file.read(buff.data(), bsize);
            adler = crc32(adler, (const unsigned char*)buff.data(), file.gcount());
         }
         std::cout << "crc32: 0x" << std::hex << adler << std::endl;
      } {boost::progress_timer timer;
         boost::crc_32_type crc32;
         std::ifstream file(fname, std::ios::binary);
         while ( file ) {
            file.read(buff.data(), bsize);
            crc32.process_bytes(buff.data(), file.gcount());
         }
         std::cout << "boost.crc: 0x" << std::hex << crc32.checksum() << std::endl;
      }
   }

   return 0;
}

/***************************************************************************/

тесты:
Цитата

crc32: 0x8805af68
3.44 s

boost.crc: 0x8805af68
6.26 s

crc32: 0x8805af68
3.35 s

boost.crc: 0x8805af68
6.21 s

crc32: 0x8805af68
3.49 s

boost.crc: 0x8805af68
6.14 s

как показали тесты, boost.crc вдвое медленнее. кто-то юзал "сие" для критичных к времени подсчетов? тормоза не мешали? как оптимизировали?

Автор: jonie 3.9.2010, 10:54
так сравнивать нельзя - может у тебя там Ось затупила при работе с жестким... надо имхо все же для чистоты с памятью работать

Автор: boostcoder 3.9.2010, 10:58
Цитата(jonie @  3.9.2010,  10:54 Найти цитируемый пост)
может у тебя там Ось затупила при работе с жестким

но не на каждой итерации же.

Цитата(jonie @  3.9.2010,  10:54 Найти цитируемый пост)
надо имхо все же для чистоты с памятью работать 

так и получается.
файл - 1.5гб. после первого чтения, он уже в кэше. иначе получается 120-130 сек.

Автор: icecrashldr 9.9.2010, 14:05
Естественно ...

Код

template < std::size_t Bits >
inline
void
crc_basic<Bits>::process_bit
(
    bool  bit
)
{
    value_type const  high_bit_mask = masking_type::high_bit;

    // compare the new bit with the remainder's highest
    rem_ ^= ( bit ? high_bit_mask : 0u );

    // a full polynominal division step is done when the highest bit is one
    bool const  do_poly_div = static_cast<bool>( rem_ & high_bit_mask );

    // shift out the highest bit
    rem_ <<= 1;

    // carry out the division, if needed
    if ( do_poly_div )
    {
        rem_ ^= poly_;
    }
}

template < std::size_t Bits >
void
crc_basic<Bits>::process_bits
(
    unsigned char  bits,
    std::size_t    bit_count
)
{
    // ignore the bits above the ones we want
    bits <<= CHAR_BIT - bit_count;

    // compute the CRC for each bit, starting with the upper ones
    unsigned char const  high_bit_mask = 1u << ( CHAR_BIT - 1u );
    for ( std::size_t i = bit_count ; i > 0u ; --i, bits <<= 1u )
    {
        process_bit( static_cast<bool>(bits & high_bit_mask) );
    }
}

template < std::size_t Bits >
inline
void
crc_basic<Bits>::process_byte
(
    unsigned char  byte
)
{
    process_bits( (rft_in_ ? detail::reflector<CHAR_BIT>::reflect(byte)
     : byte), CHAR_BIT );
}


Против

Код

unsigned long ZEXPORT crc32(crc, buf, len)
    unsigned long crc;
    const unsigned char FAR *buf;
    uInt len;
{
    if (buf == Z_NULL) return 0UL;

#ifdef DYNAMIC_CRC_TABLE
    if (crc_table_empty)
        make_crc_table();
#endif /* DYNAMIC_CRC_TABLE */

#ifdef BYFOUR
    if (sizeof(void *) == sizeof(ptrdiff_t)) {
        u4 endian;

        endian = 1;
        if (*((unsigned char *)(&endian)))
            return crc32_little(crc, buf, len);
        else
            return crc32_big(crc, buf, len);
    }
#endif /* BYFOUR */
    crc = crc ^ 0xffffffffUL;
    while (len >= 8) {
        DO8;
        len -= 8;
    }
    if (len) do {
        DO1;
    } while (--len);
    return crc ^ 0xffffffffUL;
}

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)