std::string num2str(int64_t p_vint, int8_t p_radix) { char str[48] = { 0 }; int64_t temp = 0; int64_t o_length = 0; bool is_negative = false; //for hex //judge if is negative number str[0] = '+'; if (p_vint < 0) { str[0] = '-'; p_vint = 0 - p_vint; is_negative = true; } if (p_radix > 10) { for (int64_t i = 1; p_vint > 0; ++o_length, ++i) { temp = p_vint; p_vint /= p_radix; str[i] = temp - p_vint * p_radix + 0x30; str[i] = (str[i] > 0x39 ? (str[i] + 7) : str[i]); } } else { int a = 0; for (int64_t i = 1; p_vint > 0; ++o_length, ++i) { temp = p_vint; p_vint /= p_radix; a = temp - p_vint * p_radix + 0x30; str[i] = a; } } for (int64_t i = 1; i < (o_length / 2 + 1); i++) { str[i] ^= str[o_length + 1 - i]; str[o_length + 1 - i] ^= str[i]; str[i] ^= str[o_length + 1 - i]; } if (!is_negative) { str[o_length + 1] = 0; char * str_tmp = &str[1]; return std::string(str_tmp); } else { str[++o_length] = 0; } return std::string(str); }
Running Time:
#include <chrono> #include <string.h> using namespace std::chrono; int main() { auto start = system_clock::now(); char * str = new char[48]; memset(str, 0, 48); //for (int i = 0; i < 1000000; i++) //{ num2str(6400, 10, str); std::cout << str; //} auto usage = duration_cast<milliseconds>(system_clock::now() - start); std::cout << "Running Time:" << (double)usage.count() << "ms" << std::endl; getchar(); return 0; }
/* 2. #include <ctime> clock_t start = clock(); clock_t end = clock(); std::cout << "Running time:" << (double)(end - start) / CLOCKS_PER_SEC * 1000 << "ms" << std::endl; */
//3.No Recommanded:
#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;