File: average.cpp

package info (click to toggle)
boost1.90 1.90.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 593,156 kB
  • sloc: cpp: 4,190,642; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,776; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (86 lines) | stat: -rw-r--r-- 2,420 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2017, 2018 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#include <boost/hash2/fnv1a.hpp>
#include <boost/hash2/siphash.hpp>
#include <boost/hash2/xxhash.hpp>
#include <boost/hash2/md5.hpp>
#include <boost/hash2/sha1.hpp>
#include <boost/hash2/sha2.hpp>
#include <boost/hash2/ripemd.hpp>
#include <boost/hash2/hash_append.hpp>
#include <boost/hash2/get_integral_result.hpp>
#include <boost/core/type_name.hpp>
#include <chrono>
#include <typeinfo>
#include <cstddef>
#include <cstdio>
#include <string>
#include <limits>
#include <cmath>

template<class R, class H> void test_( int N )
{
    typedef std::chrono::steady_clock clock_type;

    clock_type::time_point t1 = clock_type::now();

    double r = 0;

    H h;

    for( int i = 0; i < N; ++i )
    {
        using boost::hash2::get_integral_result;
        r += get_integral_result<R>( h );
        r += 0.5;
    }

    clock_type::time_point t2 = clock_type::now();

    long long ms = std::chrono::duration_cast<std::chrono::milliseconds>( t2 - t1 ).count();

    r /= N;

    // Standard deviation of Bates distribution
    double stddev = static_cast<double>( std::numeric_limits<R>::max() ) - static_cast<double>( std::numeric_limits<R>::min() ) / std::sqrt( 12.0 * N );

    r /= stddev;

    printf( "%s: r=%e, %lld ms\n", boost::core::type_name<H>().c_str(), r, ms );
}

template<class R> void test( int N )
{
    printf( "Integral result type `%s`:\n\n", boost::core::type_name<R>().c_str() );

    test_<R, boost::hash2::fnv1a_32>( N );
    test_<R, boost::hash2::fnv1a_64>( N );
    test_<R, boost::hash2::xxhash_32>( N );
    test_<R, boost::hash2::xxhash_64>( N );
    test_<R, boost::hash2::siphash_32>( N );
    test_<R, boost::hash2::siphash_64>( N );
    test_<R, boost::hash2::md5_128>( N );
    test_<R, boost::hash2::sha1_160>( N );
    test_<R, boost::hash2::sha2_256>( N );
    test_<R, boost::hash2::sha2_224>( N );
    test_<R, boost::hash2::sha2_512>( N );
    test_<R, boost::hash2::sha2_384>( N );
    test_<R, boost::hash2::sha2_512_224>( N );
    test_<R, boost::hash2::sha2_512_256>( N );
    test_<R, boost::hash2::ripemd_160>( N );
    test_<R, boost::hash2::ripemd_128>( N );

    puts( "" );
}

int main()
{
    int const N = 64 * 1024 * 1024;

    test<signed char>( N );
    test<short>( N );
    test<int>( N );
    test<long long>( N );
}