C++ uuid_generate time cost

//Util.h
#ifndef Util_H
#define Util_H #include <functional> #include <iostream> #include <mysql/mysql.h> #include <random> #include <sstream> #include <string.h> #include <thread> #include <uuid/uuid.h> #include <vector> #include "Model/Geeks.h" #include "Model/Parent.h" #include "Model/BookStruct.cpp" class Util { public: static char *uuidValue; static char *dtValue; void testTimeUuid(int loops); char *getUuid(); void printTimeCost13(chrono::time_point<chrono::system_clock> startTime, chrono::time_point<chrono::system_clock> endTime); }; #endif

//Util.cpp

#include "Model/Util.h"

char *Util::uuidValue = (char *)malloc(40);
char *Util::dtValue = (char *)malloc(20);

void Util::testTimeUuid(int loops)
{
    int interval=1000000;
    chrono::time_point<chrono::system_clock> startTime;
    chrono::time_point<chrono::system_clock> endTime;
    for(int i=0;i<loops;i++)
    {
        startTime=chrono::system_clock::now();
        for(int j=0;j<interval;j++)
        {
            getUuid();
        }
        endTime=chrono::system_clock::now();
        cout<<"Loop="<<i<<endl;
        printTimeCost13(startTime,endTime);
    }
}

char *Util::getUuid()
{
    uuid_t newUUID;
    uuid_generate(newUUID);
    uuid_unparse(newUUID, uuidValue);
    return uuidValue;
}

void Util::printTimeCost13(chrono::time_point<chrono::system_clock> startTime,
                           chrono::time_point<chrono::system_clock> endTime)
{ 
    cout <<"Time cost:"
         << chrono::duration_cast<chrono::seconds>(endTime - startTime).count() << " seconds,"
         << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " milliseconds,"
         << chrono::duration_cast<chrono::microseconds>(endTime - startTime).count() << " milliseconds,"
         << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!"
         << endl;
}

 

//h1.cpp

#include "Model/Util.h"

void uuidTest13(int loops);

int main(int args,char **argv)
{
    try
    {
        uuidTest13(atoi(argv[1]));
    }
    catch(const std::exception& e)
    {
        std::cerr << e.what() << '\n';
    } 
} 

void uuidTest13(int loops)
{
    Util ul;
    ul.testTimeUuid(loops);
}

 

Complile:

g++ -g -std=c++2a -I. *.cpp ./Model/*.cpp -o h1 -lmysqlclient -luuid -lpthread;

 

Execute in Win10+VMWare WorkStation Pro+Ubuntu+g++

 

posted @ 2022-05-24 08:27  FredGrit  阅读(170)  评论(0编辑  收藏  举报