首页 > 代码库 > [C++] Running time and Integer to String

[C++] Running time and Integer to String


#pragma once
#include "Inf.hpp"
#include <stdint.h>
#include <string>
#include <iostream>
#include <sstream>

#define BUFFER_SIZE 1048576

std::string int2str(int32_t p_vint);
std::string int2str_oss(int32_t p_vint);

std::string int2str(int32_t p_vint)
{
    char str[11] = { 0 };
    int bits = 0, temp = 0;
    for (int i = 0; p_vint > 1; ++bits, ++i)
    {
        temp = p_vint;
        p_vint /= 10;
        str[i] = temp - p_vint * 10 + 0x30;
    }
    
    for (int i = 0; i < bits / 2; i++)
    {
        str[i] ^= str[bits - 1 - i];
        str[bits - 1 - i] ^= str[i];
        str[i] ^= str[bits - 1 - i];
    }

    str[bits] = 0;
    return std::string(str);
}

std::string int2str_oss(int32_t p_vint)
{ 
    std::ostringstream oss;
    oss << p_vint;
    return oss.str();
}

  

Running Time:

#include <Windows.h>

int main()
{
    double time = 0;
    double counts = 0;
    LARGE_INTEGER nFreq;
    LARGE_INTEGER nBeginTime;
    LARGE_INTEGER nEndTime;
    QueryPerformanceFrequency(&nFreq);
    QueryPerformanceCounter(&nBeginTime);//开始计时  
    //encode("d:/bgm_.txt", "d:/bgm2.txt");

    //test
    //for (int i = 0; i < 1000000; i++)
    //    int2str(i);   //5.33s
    //for (int i = 0; i < 1000000; i++)
    //    int2str_oss(i);   //23.53s

    QueryPerformanceCounter(&nEndTime);//停止计时  
    time = (double)(nEndTime.QuadPart - nBeginTime.QuadPart) / (double)nFreq.QuadPart;
    std::cout << "Running Time:" << time * 1000 << "ms" << std::endl;

    return 0;

  

[C++] Running time and Integer to String