UUID performance generation uuid performance problems

Source: Internet
Author: User
Tags 04x random seed

In the project, you need to change the original id to uuid, so you can directly use the uuid library in boost to generate a uuid.

However, during the test, it was found that the program performance was severely degraded. In the past, 0.6 million pieces of data could be processed per minute, and only about 20 thousand pieces of data could be processed after uuid was used.

Follow up and let's first look at our implementation method:

1 static std::string uuid()2 {3     boost::uuids::uuid uuid = boost::uuids::random_generator()();4     return boost::lexical_cast<std::string>(uuid);5 }6                 

According to the test, the uuid generation speed is about 1000/s, which is too inefficient!

 

Google found that the problem may be the Random Seed when uuid is generated:

The requirement for uuid: create to be fast conflicts with the requirement that it needs to create a "universally unique identifier", that is, a high-quality random number. typically, high-quality randomness is more important than performance, as one rarely needs to create one million UUIDs. one coshould argue that the high-performance case is adequately addressed
The engine-taking overload.

Http://boost.2283326.n4.nabble.com/uuid-uuid-create-is-extremely-slow-td2627839.html

 

Finally, my colleague changed to the following:

static std::string uuid(){        static time_t t = time(NULL);        boost::mt19937 ran;        ran.seed(t++);        boost::uuids::basic_random_generator<boost::mt19937> gen(&ran);        boost::uuids::uuid u = gen();        return boost::lexical_cast<std::string>(u);}

Using your random seed appropriately reduces the quality of uuid generation. At last, the uuid generation speed is about 60000/s, which is acceptable.

 

I also tried uuid-dev. First install the Library:

apt-get install uuid-dev

Test code:

#include <iostream>#include <uuid/uuid.h>#include <stdio.h>using namespace std;typedef struct _GUID{    unsigned long Data1;    unsigned short Data2;    unsigned short Data3;    unsigned char Data4[8];} GUID, UUID;std::string GuidToString(const GUID &guid){    char buf[64] = {0};    snprintf(                buf,                sizeof(buf),                 "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",                guid.Data1, guid.Data2, guid.Data3,                guid.Data4[0], guid.Data4[1],                guid.Data4[2], guid.Data4[3],                guid.Data4[4], guid.Data4[5],                guid.Data4[6], guid.Data4[7]);        return std::string(buf);}int main(int argc, void** argv){        GUID guid;        time_t t = time(NULL);        for(int i=0; i<1000000; i++)        {                uuid_generate(reinterpret_cast<unsigned char *>(&guid));        }        std::cout<<time(NULL)-t<<std::endl;}

G ++-o test. cpp-luuid

The testing result is 0.2 million/second, which is faster.

 

-------------------------------------------------------------------------

Update:

In a multi-threaded environment, the uuid generated by boost is repeated. It is assumed that the random seed cannot be re-entered, and the uuid is not further explored.

Finally, uuid-dev is used. This problem is not found in the tested multithreading.

 

 

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.