• Windows c++ generate uuid via rpcrt4.lib and free RPC_CSTR via RpcStringFreeA


    // ConsoleApplication3.cpp : This file contains the 'main' function. Program execution begins and ends there.
    //
    
    #pragma comment(lib, "rpcrt4.lib") 
    #include <windows.h>
    #include <rpcdce.h>
    #include <chrono>
    #include <ctime>
    #include <fstream>
    #include <iostream> 
    #include <sstream> 
    
    using namespace std;
     
    string getUuid()
    {
        UUID newUUID;
        RPC_CSTR uuidStr;
        string uuidValue;
        if (UuidCreate(&newUUID) != RPC_S_OK)
        {
            cout << "Couldn't create uuid " << GetLastError() << endl;
        }
    
        if (UuidToStringA(&newUUID, &uuidStr) != RPC_S_OK)
        {
            cout << "Couldn't convert uuid to string " << GetLastError() << endl;
        }
        uuidValue = (char*)uuidStr;
        RpcStringFreeA(&uuidStr);
        return uuidValue;
    }
    
    void logFile(string fileName, int loops)
    {
        fstream wFile(fileName, ios::app);
        if (!wFile.is_open())
        {
            cout << "Create or open " << fileName << " failed!" << endl;
            return;
        }
    
        uint64_t num = 0;
        stringstream ss;
        chrono::time_point<chrono::high_resolution_clock> startTime, endTime;
        for (int i = 0; i < loops; i++)
        {
            startTime = chrono::high_resolution_clock::now();
            ss = stringstream();
            for (int j = 0; j < 1000000; j++)
            { 
                ss << ++num << "," << getUuid() << endl;
            }
            wFile << ss.str();
            ss.str();
            if (!wFile.good())
            {
                cout << num << ",write failed!" << endl;
                break;
            } 
            endTime = chrono::high_resolution_clock::now();
            cout << num << ","
                << chrono::duration_cast<chrono::seconds>(endTime - startTime).count() << " seconds,"
                << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " milliseconds,"
                << chrono::duration_cast<chrono::milliseconds>(endTime - startTime).count() << " microseconds,"
                << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!" << endl << endl;
        }
    
        wFile.close();
        cout << num << ",finished in " << __FUNCTION__ << endl;
    }
    
    int main()
    {
        logFile("Log.txt", 10000000);
    }
    
    // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
    // Debug program: F5 or Debug > Start Debugging menu
    
    // Tips for Getting Started: 
    //   1. Use the Solution Explorer window to add/manage files
    //   2. Use the Team Explorer window to connect to source control
    //   3. Use the Output window to see build output and other messages
    //   4. Use the Error List window to view errors
    //   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
    //   6. In the future, to open this project again, go to File > Open > Project and select the .sln file
  • 相关阅读:
    设计模式面试题(总结最全面的面试题!!!)
    一文彻底搞懂CAS实现原理
    从前慢ShardingJDBC
    Azure Storage (30) Azure Storage费用和事务相关的问题
    数据库界的Swagger:一键生成数据库文档!
    异步编程利器:CompletableFuture
    Android recyclerview的滑动到指定的item
    android 获取 item的位置,RecyclerView 滚动和获取指定位置Item的完整方案
    Android Recyclerview适配器 加载头部 以及自定义View
    基础python的快速学习
  • 原文地址:https://www.cnblogs.com/Fred1987/p/16767292.html
Copyright © 2020-2023  润新知