首页 > 编程语言 >C++ write file hugely

C++ write file hugely

时间:2022-10-07 20:33:05浏览次数:48  
标签:count char include hugely startTime write window file endTime

// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#pragma once
#pragma comment(lib,"rpcrt4.lib")
#define _CRT_SECURE_NO_WARNINGS
#include <chrono>
#include <ctime>
#include <fstream>
#include <iostream>
#include <sstream>
#include <Windows.h>

using namespace std;

static char* uuidValue;
static char* dtValue = (char*)malloc(20);

char* getUuid()
{
    UUID newUUID;
    UuidCreate(&newUUID);
    UuidToStringA(&newUUID, (RPC_CSTR*)&uuidValue);
    return uuidValue;
}

char* getTimeNow()
{
    time_t rawTime = time(nullptr);
    struct tm tmInfo = *localtime(&rawTime);
    strftime(dtValue, 20, "%Y%m%d%H%M%S", &tmInfo);
    return dtValue;
}

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++)
    {
        ss = stringstream();
        uuidValue = (char*)malloc(40);
        startTime = chrono::high_resolution_clock::now();
        for (int j = 0;j < 1000000;j++)
        {
            ss << ++num << "," << getUuid() << endl;
        }
        wFile << ss.str();
        if (!wFile.good())
        {
            cout << num << "," << getTimeNow() << ",write failed!" << endl;
            return;
        }
        free(uuidValue);
        ss = stringstream(string());
        endTime = chrono::high_resolution_clock::now();
        cout << num << "," << getTimeNow() << ",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() << " microseconds,"
            << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!" << endl << endl;
    }
    
    wFile.close();
    cout << num << "," << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}

void readFile(string fileName)
{
    fstream rFile(fileName, ios::in);
    if (!rFile.is_open())
    {
        cout << "Open " << fileName << "failed!" << endl;
        return;
    }

    uint64_t num = 0;
    string line;
    chrono::time_point<chrono::high_resolution_clock> startTime, endTime;
    startTime = chrono::high_resolution_clock::now();
    while (getline(rFile, line))
    {
        if (++num % 10000000 == 0)
        {
            endTime = chrono::high_resolution_clock::now();
            cout << num<<","<<getTimeNow() << "," << line << ",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() << " microseconds,"
                << chrono::duration_cast<chrono::nanoseconds>(endTime - startTime).count() << " nanoseconds!!!" << endl << endl;
            startTime = chrono::high_resolution_clock::now();
        }
    }
    rFile.close();
    cout << num << "," << getTimeNow() << ",finished in " << __FUNCTION__ << endl;
}


int main(int args,char** argv)
{
    try
    {
        logFile(argv[1], atoi(argv[2]));
        cout << getTimeNow() << ",finished!" << endl;
    }
    catch (const std::exception&ex)
    {
        cout << getTimeNow() << "," << ex.what() << endl;
    }   
}

// 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

 

 

Although I had released the malloced memory,while it's still growing in memory.

标签:count,char,include,hugely,startTime,write,window,file,endTime
From: https://www.cnblogs.com/Fred1987/p/16761185.html

相关文章