C++ write file hugely

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

C++ write file hugely

 

 

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

原文链接: https://www.cnblogs.com/Fred1987/p/16761185.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;

也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬

    C++ write file hugely

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/401317

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年4月19日 上午9:12
下一篇 2023年4月19日 上午9:12

相关推荐