cpp generate uuid by random

发布时间 2023-07-19 11:55:11作者: Fred1987
#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;
}

 

 

#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_time_cost(const int &loops)
{
    char strUuid[37];
    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++)
        {
            srand(time(NULL));
            gen_uuid4(strUuid, sizeof(strUuid));
        }
        _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;
    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++)
        {
            srand(time(NULL));
            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

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