#ifndef _CRT_SECURE_NO_WARNINGS #define _CRT_SECURE_NO_WARNINGS #endif #include "../../../MyIntegral/MyIntegral.hpp" #include #include #include #include #include #include int main() { using ratio = std::ratio<1, static_cast(1e9)>; using duration = std::chrono::duration; using timePoint = std::chrono::time_point; timePoint start, end{}; char buffer[20]; std::string str; std::locale oNewLocale(std::locale(), "", std::locale::ctype); std::locale oPreviousLocale = std::locale::global(oNewLocale); start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { MyStd::itoa(buffer, i); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "MyStd::itoa costed " << (end - start).count() / 1e6 << "ms" << std::endl; #ifdef MyStdWindowsAPI start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { #ifdef _MSVC_LANG ::_itoa(i, buffer, 10); #else ::itoa(i, buffer, 10); #endif } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "::itoa costed " << (end - start).count() / 1e6 << "ms" << std::endl; #endif #ifdef MyStdWindowsAPI start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { ::_itoa_s(i, buffer, 10); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "::_itoa_s costed " << (end - start).count() / 1e6 << "ms" << std::endl; #endif start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { ::sprintf(buffer, "%d", i); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "::sprintf costed " << (end - start).count() / 1e6 << "ms" << std::endl; #ifdef MyStdWindowsAPI start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { ::sprintf_s(buffer, "%d", i); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "::sprintf_s costed " << (end - start).count() / 1e6 << "ms" << std::endl; #endif start = std::chrono::high_resolution_clock::now(); for (int i = 0; i < 1e4; i++) { str = std::to_string(i); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "std::to_string costed " << (end - start).count() / 1e6 << "ms" << std::endl; start = std::chrono::high_resolution_clock::now(); std::ostringstream oss; for (int i = 0; i < 1e4; i++) { oss << i; str = oss.str(); oss.str(""); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "std::ostringstream::str costed " << (end - start).count() / 1e6 << "ms" << std::endl; start = std::chrono::high_resolution_clock::now(); std::stringstream ss; for (int i = 0; i < 1e4; i++) { ss << i; ss >> str; ss.clear(); } end = std::chrono::high_resolution_clock::now(); std::cout << std::fixed << "std::ostringstream::operator>> costed " << (end - start).count() / 1e6 << "ms" << std::endl; std::ofstream out("test.txt"); out << buffer << str << std::endl; out.close(); remove("test.txt"); std::cout << "ItoaSpeedTest is successful." << std::endl; std::locale::global(oPreviousLocale); return 0; }