예제 #1
0
파일: uuid.c 프로젝트: canmor-lam/libsg
/* we make a fake uuid using random values here.
 *
 * TODO we need a full RFC 4122 4.3 implementation later
 */
static tb_bool_t tb_uuid_generate(tb_byte_t uuid[16])
{
    // disable pseudo random
    tb_random_reset(tb_false);

    // generate random values
    tb_uint32_t r0 = (tb_uint32_t)tb_random();
    tb_uint32_t r1 = (tb_uint32_t)tb_random();
    tb_uint32_t r2 = (tb_uint32_t)tb_random();
    tb_uint32_t r3 = (tb_uint32_t)tb_random();

    // fill uuid
    tb_bits_set_u32_be(uuid + 0,    r0);
    tb_bits_set_u32_be(uuid + 4,    r1);
    tb_bits_set_u32_be(uuid + 8,    r2);
    tb_bits_set_u32_be(uuid + 12,   r3);

    // ok
    return tb_true;
}
예제 #2
0
static tb_void_t tb_demo_test_cstr_h(tb_size_t index)
{
    // the count
    tb_size_t count = 1000000;

    // save func
    g_func_indx = index;
    g_func_prev = tb_item_func_str(tb_true);

    // the func
    tb_item_func_t func = g_func_prev; 
    func.hash = tb_demo_test_hash_func;

    // init filter
    tb_bloom_filter_ref_t filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_001, 1, count, func);
    if (filter)
    {
        // clear random
        tb_random_clear(tb_random_generator());

        // done
        tb_size_t i = 0;
        tb_size_t r = 0;
        tb_char_t s[256] = {0};
        tb_hong_t t = tb_mclock();
        for (i = 0; i < count; i++)
        {
            // the value
            tb_long_t value = tb_random();

            // format it
            tb_snprintf(s, sizeof(s) - 1, "%ld", value);

            // set value to filter
            if (!tb_bloom_filter_set(filter, s))
            {
                // repeat++
                r++;
            }
        }
        t = tb_mclock() - t;

        // trace
        tb_trace_i("cstr: index: %lu, repeat: %lu, time: %lld ms", index, r, t);

        // exit filter
        tb_bloom_filter_exit(filter);
    }
}
예제 #3
0
static tb_void_t tb_demo_test_cstr_p()
{
    // the count
    tb_size_t count = 10000000;

    // init filter
    tb_bloom_filter_ref_t filter = tb_bloom_filter_init(TB_BLOOM_FILTER_PROBABILITY_0_01, 3, count, tb_item_func_str(tb_true));
    if (filter)
    {
        // clear random
        tb_random_clear(tb_random_generator());

        // done
        tb_size_t i = 0;
        tb_size_t r = 0;
        tb_char_t s[256] = {0};
        for (i = 0; i < count; i++)
        {
            // the value
            tb_long_t value = tb_random();

            // format it
            tb_snprintf(s, sizeof(s) - 1, "%ld", value);

            // set value to filter
            if (!tb_bloom_filter_set(filter, s))
            {
                // repeat++
                r++;
            }
        }

        // trace
#ifdef TB_CONFIG_TYPE_FLOAT
        tb_trace_i("cstr: count: %lu, repeat: %lu, repeat_p ~= p: %lf", count, r, (tb_double_t)r / count);
#else
        tb_trace_i("cstr: count: %lu, repeat: %lu", count, r);
#endif

        // exit filter
        tb_bloom_filter_exit(filter);
    }
}