File: char_seq.cpp

package info (click to toggle)
boost1.90 1.90.0-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 593,120 kB
  • sloc: cpp: 4,190,908; xml: 196,648; python: 34,618; ansic: 23,145; asm: 5,468; sh: 3,774; makefile: 1,161; perl: 1,020; sql: 728; ruby: 676; yacc: 478; java: 77; lisp: 24; csh: 6
file content (101 lines) | stat: -rw-r--r-- 2,170 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2022 Peter Dimov.
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt

#define _CRT_SECURE_NO_WARNINGS

#include <boost/container_hash/hash.hpp>
#include <boost/core/detail/splitmix64.hpp>
#include <boost/core/type_name.hpp>
#include <boost/config.hpp>
#include <cstddef>
#include <cstdio>
#include <cstdint>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <chrono>

// test_hash_speed

template<class T, class V> void test_hash_speed( int N, V const& v )
{
    std::vector<T> w;

    w.reserve( N );

    for( int i = 0; i < N; ++i )
    {
        w.emplace_back( v[i].begin(), v[i].end() );
    }

    typedef std::chrono::steady_clock clock_type;

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

    std::size_t q = 0;

    boost::hash<T> const h;

    for( int i = 0; i < N; ++i )
    {
        q += h( w[i] );
    }

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

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

    std::string type = boost::core::type_name<T>();

#if defined( _MSC_VER )

    std::printf( "%25s : q=%20Iu, %lld ms\n", type.c_str(), q, ms1 );

#else

    std::printf( "%25s : q=%20zu, %lld ms\n", type.c_str(), q, ms1 );

#endif
}

int main()
{
    int const N = 1048576 * 8;

    std::vector<std::string> v;

    {
        v.reserve( N );

        boost::detail::splitmix64 rnd;

        for( int i = 0; i < N; ++i )
        {
            char buffer[ 64 ];

            unsigned long long k = rnd();

            if( k & 1 )
            {
                std::snprintf( buffer, sizeof( buffer ), "prefix_%llu_suffix", k );
            }
            else
            {
                std::snprintf( buffer, sizeof( buffer ), "{%u}", static_cast<unsigned>( k ) );
            }

            v.push_back( buffer );
        }
    }

    std::puts( "Char sequence hashing test:\n" );

    test_hash_speed< std::string >( N, v );
    test_hash_speed< std::vector<char> >( N, v );
    test_hash_speed< std::deque<char> >( N, v );
    test_hash_speed< std::list<char> >( N, v );

    std::puts( "" );
}