码迷,mamicode.com
首页 > 编程语言 > 详细

[C++] Running time and Integer to String

时间:2016-12-06 20:03:22      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:its   form   str   int   log   ios   rman   end   ndt   


#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

标签:its   form   str   int   log   ios   rman   end   ndt   

原文地址:http://www.cnblogs.com/maikaze/p/6138741.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!