cpp generate uuid by random

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
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstdint>
 
uint32_t rand32()
{
    return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff);
}
 
bool gen_uuid4(char dst[37], size_t len)
{
    int n = snprintf(dst, len, "%08x-%04x-%04x-%04x-%04x%08x",
        rand32(),                         // Generates a 32-bit Hex number
        rand32() & 0xffff,                // Generates a 16-bit Hex number
        ((rand32() & 0x0fff) | 0x4000),   // Generates a 16-bit Hex number of the form 4xxx (4 indicates the UUID version)
        (rand32() & 0x3fff) + 0x8000,     // Generates a 16-bit Hex number in the range [0x8000, 0xbfff]
        rand32() & 0xffff, rand32());     // Generates a 48-bit Hex number
         
    return n >= 0 && n < len;             // Success only when snprintf result is a positive number and the provided buffer was large enough.
}
 
int main()
{
    char strUuid[37];
    srand(time(NULL));
     
    bool success = gen_uuid4(strUuid, sizeof(strUuid));
     
    printf("%s\n", success ? strUuid : "UUID generation failed!");
     
    return 0;
}

 

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <algorithm>
#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <thread>
#include <typeinfo>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cstdint>
 
std::string get_time_now()
{
    std::chrono::time_point<std::chrono::high_resolution_clock> now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    auto seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
    ss << "_"
       << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
       << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
       << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    return ss.str();
}
 
uint32_t rand32()
{
    return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff);
}
 
void gen_uuid4(char dst[37], size_t len)
{
    snprintf(dst, len, "%08x-%04x-%04x-%04x-%04x%08x",
             rand32(),                       // Generates a 32-bit Hex number
             rand32() & 0xffff,              // Generates a 16-bit Hex number
             ((rand32() & 0x0fff) | 0x4000), // Generates a 16-bit Hex number of the form 4xxx (4 indicates the UUID version)
             (rand32() & 0x3fff) + 0x8000,   // Generates a 16-bit Hex number in the range [0x8000, 0xbfff]
             rand32() & 0xffff, rand32());   // Generates a 48-bit Hex number
}
 
void test_rand_gen_uuid(const int &len)
{
    char uuid_value[37];
    srand(time(NULL));
    for (int i = 0; i < len; i++)
    {
        gen_uuid4(uuid_value, sizeof(uuid_value));
        std::cout << i << "," << uuid_value << std::endl;
    }
    std::cout << get_time_now() << ",finished in " << __FUNCTION__ << std::endl;
}
 
void test_time_cost(const int &loops)
{
    char uuid_value[37];
    srand(time(NULL));
    std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
    for (int i = 0; i < loops; i++)
    {
        _start_time = std::chrono::high_resolution_clock::now();
 
        for (int j = 0; j < 1000000; j++)
        {
            gen_uuid4(uuid_value, sizeof(uuid_value));
        }
        _end_time = std::chrono::high_resolution_clock::now();
        std::cout << "Loops:" << i + 1 << ", time cost:"
                  << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                  << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                  << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                  << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!" << std::endl;
    }
}
 
void write_file(const std::string &file_name, const int &loops)
{
    std::ofstream w_file(file_name, std::ofstream::app);
    if (!w_file.is_open())
    {
        std::cout << get_time_now() << ",create or open " << file_name << " failed!" << std::endl;
        return;
    }
 
    char uuid_value[37];
    std::stringstream ss;
    srand(time(NULL));
    static std::uint64_t num = 0;
    std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
    for (int i = 0; i < loops; i++)
    {
        _start_time = std::chrono::high_resolution_clock::now();
        for (int j = 0; j < 1000000; j++)
        {
 
            gen_uuid4(uuid_value, sizeof(uuid_value));
            ss << ++num << "," << uuid_value << std::endl;
        }
        _end_time = std::chrono::high_resolution_clock::now();
        w_file.write(ss.str().c_str(), ss.str().size());
        ss = std::stringstream();
        if (!w_file.good())
        {
            std::cout << get_time_now() << ", write failed!loops:" << i + 1 << ",num:" << num << std::endl;
            break;
        }
        std::cout << get_time_now() << ",loops:" << i + 1 << ",num:" << num << ",time cost:"
                  << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                  << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                  << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                  << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!!!" << std::endl;
    }
    w_file.close();
    std::cout << get_time_now() << ",loops:" << loops << ",num:" << num << ",finished in " << __FUNCTION__ << std::endl;
}
 
int main(int agrs, char **argv)
{
    write_file(argv[1],atoi(argv[2]));
    return 0;
}

  

Compile

1
g++-12 -std=c++2a *.cpp -o h1

  

.Run

1
./h1 log.txt 10;

  

 

 

1
grep -E '00000,' log.txt;

  

 

posted @   FredGrit  阅读(20)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
历史上的今天:
2021-07-19 Split string via delimiter and print the split string and delimiter's index
点击右上角即可分享
微信分享提示