Skip to content

Instantly share code, notes, and snippets.

@cpei-avalara
Forked from anonymous/benchmark.cpp
Created December 23, 2017 03:14
Show Gist options
  • Save cpei-avalara/8aedf14f5618852be2cff4de267d497c to your computer and use it in GitHub Desktop.
Save cpei-avalara/8aedf14f5618852be2cff4de267d497c to your computer and use it in GitHub Desktop.
The C++ Integer To String Conversion Benchmark
/*
*******************************************************************************
* The C++ Integer To String Conversion Benchmark *
* *
* Version: 2.718-20131130 *
* *
* This benchmark is based on the solutions provided in the following *
* stackoverflow question: *
* *
* http://stackoverflow.com/q/4351371 (Date: 20101204) *
* *
* Title: C++ Performance Challenge - Integer To std::string Conversion *
* *
* 0. sprintf (libc) *
* 1. karma (Boost.Spirit - http://boost-spirit.com) *
* 2. strtk (Arash Partow - http://www.partow.net) *
* 3. so (user434507) *
* 4. timo (Timo ???) *
* 5. voigt (Richard Benjamin Voigt - http://www.benvoigt.com) *
* 6. hopman_fast (Chris Hopman - http://github.com/cjhopman) *
* 7. zverovich (Victor Zverovich - http://zverovich.net) *
* 8. jiaendu (Jiaen Du aka TaoKaYan) *
* *
* RESULTS: *
* Integer To String Test *
* [sprintf ] Numbers: 200000000 Time: 51.2362sec Rate: 3903490.2974nums/sec *
* [karma ] Numbers: 200000000 Time: 11.5568sec Rate: 17305869.6655nums/sec *
* [strtk ] Numbers: 200000000 Time: 3.7986sec Rate: 52650806.8309nums/sec *
* [so ] Numbers: 200000000 Time: 6.1317sec Rate: 32617186.4989nums/sec *
* [timo ] Numbers: 200000000 Time: 4.3279sec Rate: 46211794.7588nums/sec *
* [voigt ] Numbers: 200000000 Time: 9.3068sec Rate: 21489702.1150nums/sec *
* [hopman ] Numbers: 200000000 Time: 3.8343sec Rate: 52160759.4606nums/sec *
* [zverovich] Numbers: 200000000 Time: 10.5063sec Rate: 19036262.1877nums/sec *
* [jiaendu ] Numbers: 200000000 Time: 3.2811sec Rate: 60955167.4743nums/sec *
* *
* System Details: *
* 0. Intel Xeon W3680 3.3GHz *
* 1. Mint 15 (Kernel 3.11) *
* 2. GCC 4.8, O2, PGO *
* 3. Boost 1.55.0 *
* *
* Dependencies *
* 1. Boost: http://www.boost.org *
* 2. StrTk: http://code.google.com/p/strtk *
* 3. fmt: http://github.com/vitaut/format *
* *
* Benchmark Details: *
* 1. A basic validation/sanity check is done on all entrants. *
* 2. Flushing of the i/d-caches is performed before each round. *
* *
* Note: Copyright of code portions below other than StrTk are the purview of *
* their owners - please contact them directly for usage. The benchmark itself *
* is public domain. *
* *
*******************************************************************************
*/
#include <cstddef>
#include <cstdio>
#include <iostream>
#include <string>
#include <iterator>
#define strtk_no_tr1_or_boost
#include "strtk.hpp"
#include <boost/lexical_cast.hpp>
//Note: Define USE_SPIRIT to include Karma (requires Boost 1.45+)
#define USE_SPIRIT
#ifdef USE_SPIRIT
#define INCLUDE_KARMA
#define BOOST_KARMA_NUMERICS_LOOP_UNROLL 6
#include <boost/spirit/include/karma.hpp>
#endif
#include "format.h"
static const int max_i2s = 40000000;
void print_mode(const std::string& mode)
{
static const std::size_t test_mode_length = sizeof(" ");
printf("[%s] ", strtk::text::left_align(test_mode_length, ' ', mode).c_str());
fflush(stdout);
}
#define enable_test_type01
#define enable_test_type02
#define enable_test_type03
#define enable_test_type04
#define enable_test_type05
static const int randval[] =
{
936116, 369532, 453755, -72860, 209713, 268347, 435278, -360266, -416287, -182064, -5,
944969, 640463, -366588, 471577, -69401, -744294, -505829, 923883, 831785, -601136, -636767,
-437054, 591718, 100758, 231907, -719038, 973540, -605220, 506659, -871653, 462533, 764843,
-919138, 404305, -630931, -288711, -751454, -173726, -718208, 432689, -281157, 360737, 659827,
19174, -376450, 769984, -858198, 439127, 734703, -683426, 7, 386135, 186997, -643900,
-744422, -604708, -629545, 42313, -933592, -635566, 182308, 439024, -367219, -73924, -516649,
421935, -470515, 413507, -78952, -427917, -561158, 737176, 94538, 572322, 405217, 709266,
-357278, -908099, -425447, 601119, -2, -862285, -177869, 900102, 384877, 157859, -641680,
503738, -702558, 278225, 463290, 268378, -212840, 580090, 347346, -473985, -950968, -114547,
-839893, -738032, -789424, 409540, 493495, 432099, 119755, 905004, -174834, 338266, 234298,
74641, -965136, -754593, 1, 685273, 466924, 920560, 385062, 796402, -67229, 994864,
375424, 271700, -743062, 390176, 924237, 772574, 676610, 435752, -153847, 376974, 299869,
-647540, -128724, 469890, -163167, -547803, -743363, 486463, -621028, 612288, 27459, -514224,
126342, -66612, 803409, -777155, -336453, -284002, 472451, 342390, -163630, 908356, -456147,
-825607, 268092, -974715, 287227, 227890, -524101, 616370, -782456, 922098, -624001, -813690,
171605, -192962, 796151, 707183, -95696, -23163, -721260, 508892, 430715, 791331, 482048,
-996102, 863274, 275406, -8279, -556239, -902076, 268647, -818565, 260069, -798232, -172924,
-566311, -806503, -885992, 813969, -78468, 956632, 304288, 494867, -508784, 381751, 151264,
762953, 76352, 594902, 375424, 271700, -743062, 390176, 924237, 772574, 676610, 435752,
-153847, 3959, -971937, -294181, -538049, -344620, -170136, 19120, -703157, 868152, -657961,
-818631, 219015, -872729, -940001, -956570, 880727, -345910, 942913, -942271, -788115, 225294,
701108, -517736, -416071, 281940, 488730, 942698, 711494, 838382, -892302, -533028, 103052,
528823, 901515, 949577, 159364, 718227, -241814, -733661, -462928, -495829, 165170, 513580,
-629188, -509571, -459083, 198437, 77198, -644612, 811276, -422298, -860842, -52584, 920369,
686424, -530667, -243476, 49763, 345866, -411960, -114863, 470810, -302860, 683007, -509080,
2, -174981, -772163, -48697, 447770, -268246, 213268, 269215, 78810, -236340, -639140,
-864323, 505113, -986569, -325215, 541859, 163070, -819998, -645161, -583336, 573414, 696417,
-132375, 3, -294501, 320435, 682591, 840008, 351740, 426951, 609354, 898154, -943254,
227321, -859793, -727993, 44137, -497965, -782239, 14955, -746080, -243366, 9837, -233083,
606507, -995864, -615287, -994307, 4, 602715, 770771, -315040, 610860, 446102, -307120,
710728, -362507, 798196, -170790, -568895, -869379, 62020, -316693, -837793, 644994, -590392,
-230474, -762625, -637525, 134963, -202700, -766902, -985541, 218163, 682009, 926051, 525156,
-61195, 403211, -810098, 245539, -431733, 179998, -806533, 745943, 447597, 131973, -187130,
826019, 286107, -937230, -577419, 20254, 681802, -340500, 323080, 266283, -667617, 309656,
416386, 611863, 759991, -534257, 523112, -634892, -169913, -204905, -909867, -882185, -944908,
741811, -717675, 967007, -317396, 407230, -412805, 792905, 994873, 744793, -456797, 713493,
355232, 116900, -945199, 880539, 342505, -580824, -262273, 982968, -349497, -735488, 311767,
-455191, 570918, 389734, -958386, 10262, -99267, 155481, 304210, 204724, 704367, -144893,
-233664, -671441, 896849, 408613, 762236, 322697, 981321, 688476, 13663, -970704, -379507,
896412, 977084, 348869, 875948, 341348, 318710, 512081, 6163, 669044, 833295, 811883,
708756, -802534, -536057, 608413, -389625, -694603, 541106, -110037, 720322, -540581, 645420,
32980, 62442, 510157, -981870, -87093, -325960, -500494, -718291, -67889, 991501, 374804,
769026, -978869, 294747, 714623, 413327, -199164, 671368, 804789, -362507, 798196, -170790,
-568895, -869379, 62020, -316693, -837793, 644994, -39341, -417504, -243068, -957756, 99072,
622234, -739992, 225668, 8863, -505910, 82483, -559244, 241572, 1315, -36175, -54990,
376813, -11, 162647, -688204, -486163, -54934, -197470, 744223, -762707, 732540, 996618,
351561, -445933, -898491, 486531, 456151, 15276, 290186, -817110, -52995, 313046, -452533,
-96267, 94470, -500176, -818026, -398071, -810548, -143325, -819741, 1338, -897676, -101577,
-855445, 37309, 285742, 953804, -777927, -926962, -811217, -936744, -952245, -802300, -490188,
-964953, -552279, 329142, -570048, -505756, 682898, -381089, -14352, 175138, 152390, -582268,
-485137, 717035, 805329, 239572, -8, 209643, -184403, -385864, 675086, 819648, 629058,
-527109, -488666, -171981, 532788, 552441, 174666, 984921, 766514, 758787, 716309, 338801,
-978004, -412163, 876079, -734212, 789557, -160491, -522719, 56644, -991, -286038, -53983,
663740, 809812, 919889, -717502, -137704, 220511, 184396, -825740, -588447, 430870, 124309,
135956, 558662, -307087, -788055, -451328, 812260, 931601, 324347, -482989, -117858, -278861,
189068, -172774, 929057, 293787, 198161, -342386, -47173, 906555, -759955, -12779, 777604,
-97869, 899320, 927486, -25284, -848550, 259450, -485856, -17820, 88, 171400, 235492,
-326783, -340793, 886886, 112428, -246280, 5979, 648444, -114982, 991013, -56489, -9497,
419706, 632820, -341664, 393926, -848977, -22538, 257307, 773731, -905319, 491153, 734883,
-868212, -951053, 644458, -580758, 764735, 584316, 297077, 28852, -397710, -953669, 201772,
879050, -198237, -588468, 448102, -116837, 770007, -231812, 642906, -582166, -885828, 9,
305082, -996577, 303559, 75008, -772956, -447960, 599825, -295552, 870739, -386278, -950300,
485359, -457081, 629461, -850276, 550496, -451755, -620841, -11766, -950137, 832337, 28711,
-273398, -507197, 91921, -271360, -705991, -753220, -388968, 967945, 340434, -320883, -662793,
-554617, -574568, 477946, -6148, -129519, 689217, 920020, -656315, -974523, -212525, 80921,
-612532, 645096, 545655, 655713, -591631, -307385, -816688, -618823, -113713, 526430, 673063,
735916, -809095, -850417, 639004, 432281, -388185, 270708, 860146, -39902, -786157, -258180,
-246169, -966720, -264957, 548072, -306010, -57367, -635665, 933824, 70553, -989936, -488741,
72411, -452509, 529831, 956277, 449019, -577850, -360986, -803418, 48833, 296073, 203430,
609591, 715483, 470964, 658106, -718254, -96424, 790163, 334739, 181070, -373578, 5,
-435088, 329841, 330939, -256602, 394355, 912412, 231910, 927278, -661933, 788539, -769664,
-893274, -96856, 298205, 901043, -608122, -527430, 183618, -553963, -35246, -393924, 948832,
-483198, 594501, 35460, -407007, 93494, -336881, -634072, 984205, -812161, 944664, -31062,
753872, 823933, -69566, 50445, 290147, 85134, 34706, 551902, 405202, -991246, -84642,
154341, 316432, -695101, -651588, -5030, 137564, -294665, 332541, 528307, -90572, -344923,
523766, -758498, -968047, 339028, 494578, 593129, -725773, 31834, -718406, -208638, 159665,
-2043, 673344, -442767, 75816, 755442, 769257, -158730, -410272, 691688, 589550, -878398,
-184121, 460679, 346312, 294163, -544602, 653308, 254167, -276979, 52073, -892684, 887653,
-41222, 983065, -68258, -408799, -99069, -674069, -863635, -32890, 622757, -743862, 40872,
-4837, -967228, 522370, -903951, -818669, 524459, 514702, 925801, 20007, -299229, 579348,
626021, 430089, 348139, -562692, -607728, -130606, -928451, -424793, -458647, -448892, -312230,
143337, 109746, 880042, -339658, -785614, 938995, 540916, 118429, 661351, -402967, 404729,
-40918, -976535, 743230, 713110, 440182, -381314, -499252, 74613, 193652, 912717, 491323,
583633, 324691, 459397, 281253, 195540, -2764, -888651, 892449, 132663, -478373, -430002,
-314551, 527826, 247165, 557966, 554778, 481531, -946634, 431685, -769059, -348371, 174046,
184597, -354867, 584422, 227390, -850397, -542924, -849093, -737769, 325359, 736314, 269101,
767940, 674809, 81413, -447458, 445076, 189072, 906218, 502688, -718476, -863827, -731381,
100660, 623249, 710008, 572060, 922203, 685740, 55096, 263394, -243695, -353910, -516788,
388471, 455165, 844103, -643772, 363976, 268875, -899450, 104470, 104029, -238874, -274659,
732969, -676443, 953291, -916289, -861849, -242344, 958083, -479593, -970395, 799831, 277841,
-243236, -283462, -201510, 166263, -259105, -575706, 878926, 891064, 895297, 655262, -34807,
-809833, -89281, 342585, 554920, 1, 902141, -333425, 139703, 852318, -618438, 329498,
-932596, -692836, -513372, 733656, -523411, 85779, 500478, -682697, -502836, 138776, 156341,
-420037, -557964, -556378, 710993, -50383, -877159, 916334, 132996, 583516, -603392, -111615,
-12288, -780214, 476780, 123327, 137607, 519956, 745837, 17358, -158581, -53490
};
static const int rand_large_val[] =
{
-1554001998, 1675823847, -1208726812, 1401446496, -1843840247, 1251725823, -2114465251, 1139056186, -1927150976, 1855342091,
1763506982, -1798508119, 1115326656, -1733139029, 1738740180, -1276940782, 1440666696, -1996220476, 1705990921, -1982322287,
-1138468533, 1128801068, -1122385662, 2025200537, -1605897675, 2020410320, -1554316161, 2039708610, -1997042438, 1636077199,
1307005874, -1280793997, 1532742659, -1218355755, 1628958327, -1897667731, 2130121096, -2112927094, 2023007553, -1606043454,
-1156203585, 2012880672, -2131102571, 2063517219, -1564104137, 1551787808, -1929074857, 1705003165, -1911144971, 1592478462,
1561724397, -1833026888, 1704472533, -1999547316, 1681280183, -1376176522, 1240457312, -2127573722, 2061959521, -1995492868,
-1805489561, 1532144210, -1620352533, 2132297577, -1526370747, 1592829843, -1385549923, 2136032443, -1911236947, 1329544328,
1479302442, -1375378829, 1939439085, -1923737220, 1563696497, -1428571271, 1800390189, -1802324292, 1920125933, -1200247074,
-2056365692, 1544739020, -1762336553, 2075925888, -1834483027, 1723219571, -1935459071, 2024807299, -1427188201, 1240952207,
1246354109, -1348518903, 1278845135, -1836581204, 1827630334, -1490669888, 1172361532, -1602144881, 1842465746, -1190899727,
-1794332854, 1951485086, -2038373046, 1438782294, -1816029817, 2111515800, -1991589090, 1925058449, -1623494878, 1223433135,
1296403455, -2103056237, 1199499509, -1916651125, 1455679206, -1630859744, 1265058618, -2087802842, 1871448758, -2002338804,
-1531833363, 2054759153, -1751419349, 1411707104, -1305848441, 1867506295, -2054279264, 1293876654, -2082560707, 1282944828,
1661768074, -1958748298, 2094646432, -1129989185, 1508818986, -1271325250, 1511319431, -1578846735, 2059312813, -2012453977,
-1740013115, 1562983797, -1544824376, 1232156090, -1178237847, 1878518686, -1269693447, 2038920043, -1353276250, 1143771270,
2139508614, -1863209551, 1591699273, -1407171724, 1974366473, -2134909969, 1488871032, -1676745851, 1347232005, -2133591688,
-1470058701, 2020289281, -1306112735, 2103767086, -1734338657, 2005284887, -1467695848, 1315627289, -1616042353, 1908605134,
1300391123, -1280094632, 2133431116, -1615238703, 2081829273, -1848477329, 1768085833, -1325910229, 1655151509, -1979431377,
-1443232300, 1845603931, -1347848083, 1562240084, -1124194952, 1846376279, -1146490127, 1405369687, -1467967905, 1512567701,
2064883899, -1913688273, 2109499729, -1590771791, 2071726068, -1214745462, 1267912488, -2094793131, 1207037625, -1236041253,
-1850249302, 1265379007, -1682961717, 1729919468, -1269234667, 2049640712, -1735836129, 1524641552, -1321713888, 1427775703,
1689124916, -1478634026, 1180238443, -1600843251, 1662646158, -1966754235, 1557787751, -1477864335, 1662489097, -1708839869,
-1383712105, 1391729153, -1758323324, 1900337113, -1444843586, 1842714327, -1262625088, 2078154040, -1654276720, 2009233257,
1426281193, -1808730289, 2050602032, -1489478534, 1139497846, -1606756994, 1596467510, -1569369490, 1834817736, -1324404966,
-2106875309, 1162952044, -1901203598, 1255321602, -1475378652, 1907334688, -1283721176, 1183274781, -1762343221, 1578268022,
1330720102, -1826858316, 1224689167, -1689173331, 1209703738, -1343020423, 1885512356, -1386513147, 2138663394, -1810102774,
-1542111112, 1369740075, -2030118051, 1477259902, -2112057035, 1901417427, -1343040761, 1523144689, -1765976375, 1789008442,
1355760068, -1249301616, 1170801738, -1233943424, 1895130568, -1184810595, 1556300759, -1207317080, 1990842649, -1207724267,
-2081177026, 2074243318, -1924657305, 2109037603, -1669877932, 1235982707, -1464026603, 1483597757, -1946668718, 1164696588,
1621110739, -1324991216, 1349416051, -2092021489, 1359227577, -1217566923, 1145239018, -1211911764, 1488941346, -1222027550,
-2104792360, 1451710977, -1597801942, 1870061653, -1846214484, 2061266671, -2041631379, 1238134384, -1623133648, 1560156598,
1924253971, -1259663170, 1422991329, -1685241658, 1573995650, -1396982578, 1365952558, -1343929902, 1562365749, -2061014292,
-1388839178, 1216249339, -1862061324, 1941159130, -1499071457, 1766576766, -2003900558, 1498024930, -2114453414, 2031085583,
1596793582, -1967353550, 1492992631, -2117089375, 1935215648, -1482095079, 1929700104, -2028969273, 1252859933, -1680944122,
-1894469771, 1674042195, -1830961118, 1773477803, -1607396793, 1471218900, -1608271279, 1119297382, -1390907189, 1632796972,
1513066344, -1592590536, 1882954211, -1182187895, 1712328721, -1244684167, 1659462438, -2024468956, 1570632473, -1799537744,
-1761398175, 2031050921, -1214106507, 2147375467, -1943776414, 2062077492, -1727717949, 1950080074, -2141100514, 1864444123,
1866656116, -1344672802, 2145259697, -1346587050, 1844126280, -1529702246, 1161394750, -2039784734, 1394628393, -2075992860,
-1491538146, 1924012694, -1866909930, 1320537770, -2140088210, 1654409104, -1738370725, 2130756642, -1426304079, 1125446529,
1312961417, -1375366269, 1738456982, -2026140408, 1114108235, -1655262373, 1298937883, -1601737794, 1970541939, -1902410580,
-1564643524, 1320912800, -1771348462, 1559004643, -1156032031, 2124610885, -1174560758, 1220904618, -1885480403, 1583302137,
1685131539, -1186855880, 2097797837, -1758908241, 1409211947, -1500059148, 1189577863, -1544676695, 1716460869, -1899781738,
-1312540789, 1850819289, -1561123171, 1315766720, -2008893295, 1712161724, -1356949530, 1420106028, -2048995688, 1795811263,
1982941157, -1366490592, 1204526380, -1298408716, 1907803569, -1544190257, 1390435099, -1558935563, 1233508320, -1919278797,
-1587109000, 1166119151, -1441279433, 1297588866, -1898718349, 1821557437, -1718206971, 1831924231, -1996401870, 1737556980,
1920344275, -1439122148, 2012630153, -1904165814, 2047049993, -1942074247, 1516073030, -1817476344, 1603227151, -1511684437,
-1539732900, 1276154639, -1742900809, 1169360786, -1377629626, 1422638128, -1407302831, 1944457496, -1276453053, 1304626374,
1383036410, -1599214174, 1908241659, -1983125712, 2040715963, -1975140221, 2013008392, -1349696347, 1433842197, -1657204379,
-1288370225, 1519651799, -1419310416, 1209909055, -2079867106, 1857373969, -1721129645, 1902893895, -1746926208, 1417427864,
1479162420, -1692913495, 1206102602, -1761884007, 1302999946, -1686474118, 1971270433, -1544679430, 2035833653, -1461519586,
-2096069435, 1177490542, -1937970071, 2132500169, -1555221216, 1810008810, -1828292542, 1595610810, -1661475184, 2021792927,
1834777148, -1408754528
};
static const int edgeval[] =
{
std::numeric_limits<int>::max() - 0, std::numeric_limits<int>::min() + 0,
std::numeric_limits<int>::max() - 2, std::numeric_limits<int>::min() + 2,
std::numeric_limits<int>::max() - 4, std::numeric_limits<int>::min() + 4,
std::numeric_limits<int>::max() - 6, std::numeric_limits<int>::min() + 6,
std::numeric_limits<int>::max() - 1, std::numeric_limits<int>::min() + 1,
std::numeric_limits<int>::max() - 3, std::numeric_limits<int>::min() + 3,
std::numeric_limits<int>::max() - 5, std::numeric_limits<int>::min() + 5,
std::numeric_limits<int>::max() - 7, std::numeric_limits<int>::min() + 7,
std::numeric_limits<int>::max() - 1000, std::numeric_limits<int>::min() + 1000,
std::numeric_limits<int>::max() - 1200, std::numeric_limits<int>::min() + 1200,
std::numeric_limits<int>::max() - 1400, std::numeric_limits<int>::min() + 1400,
std::numeric_limits<int>::max() - 1600, std::numeric_limits<int>::min() + 1600,
std::numeric_limits<int>::max() - 1100, std::numeric_limits<int>::min() + 1100,
std::numeric_limits<int>::max() - 1300, std::numeric_limits<int>::min() + 1300,
std::numeric_limits<int>::max() - 1500, std::numeric_limits<int>::min() + 1500,
std::numeric_limits<int>::max() - 1700, std::numeric_limits<int>::min() + 1700
};
double test_multiplier()
{
return (
#ifdef enable_test_type01
+1
#endif
#ifdef enable_test_type02
+1
#endif
#ifdef enable_test_type03
+1
#endif
#ifdef enable_test_type04
+1
#endif
#ifdef enable_test_type05
+1
#endif
);
}
void flush_cache()
{
const std::size_t size = 64 * 1024 * 1024; //64MB
char* d0 = new char[size];
char* d1 = new char[size];
unsigned int total = 0;
double total_d = 0;
for (std::size_t i = 1; i < size; ++i)
{
total += d0[i] + d1[i] - (d0[i - i] ^ d1[i - i]);
const double f0 = 2312312.312321321 * (double)(d1[i - 1]);
const double f1 = 1313131.797979797 * (double)(d0[i - 1]);
if ((i & 0x01) && ((f0 - f1 + total_d) != 123.123))
d1[i - 1] = d0[i - 1] = d0[i] ^ d1[i];
else
{
d1[i - 1] = d0[i - 1] = d0[i] * d1[i];
total_d += std::min(f0,f1);
}
}
if ((total == 42) || (total_d == 42.42))
{
printf("\r");
}
delete [] d0;
delete [] d1;
}
void sprintf_lexical_cast_test_i2s()
{
print_mode("sprintf");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
s.resize(sprintf(const_cast<char*>(s.c_str()),"%d",((i & 1) ? i : -i)));
total_length += s.size();
#endif
#ifdef enable_test_type02
s.resize(sprintf(const_cast<char*>(s.c_str()),"%d",max_i2s + i));
total_length += s.size();
#endif
#ifdef enable_test_type03
s.resize(sprintf(const_cast<char*>(s.c_str()),"%d",randval[(max_i2s + i) & 1023]));
total_length += s.size();
#endif
#ifdef enable_test_type04
s.resize(sprintf(const_cast<char*>(s.c_str()),"%d",rand_large_val[(max_i2s + i) & 511]));
total_length += s.size();
#endif
#ifdef enable_test_type05
s.resize(sprintf(const_cast<char*>(s.c_str()),"%d",edgeval[(max_i2s + i) & 31]));
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
void boost_lexical_cast_test_i2s()
{
print_mode("boost");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
s = boost::lexical_cast<std::string>(((i & 1) ? i : -i));
total_length += s.size();
#endif
#ifdef enable_test_type02
s = boost::lexical_cast<std::string>(max_i2s + i);
total_length += s.size();
#endif
#ifdef enable_test_type03
s = boost::lexical_cast<std::string>(randval[(max_i2s + i) & 1023]);
total_length += s.size();
#endif
#ifdef enable_test_type04
s = boost::lexical_cast<std::string>(rand_large_val[(max_i2s + i) & 511]);
total_length += s.size();
#endif
#ifdef enable_test_type05
s = boost::lexical_cast<std::string>(edgeval[(max_i2s + i) & 31]);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
#ifdef INCLUDE_KARMA
#include <boost/spirit/include/karma.hpp>
inline bool karma_int_to_string(const int& value, std::string& str)
{
using namespace boost::spirit;
using boost::spirit::karma::generate;
char buffer[16];
char* x = buffer;
if (!generate(x, int_, value))
return false;
str.assign(buffer, x - buffer);
return true;
}
void karma_lexical_cast_test_i2s()
{
print_mode("karma");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
karma_int_to_string(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
karma_int_to_string(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
karma_int_to_string(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
karma_int_to_string(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
karma_int_to_string(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
#else
void karma_lexical_cast_test_i2s(){}
#endif
void strtk_lexical_cast_test_i2s()
{
print_mode("strtk");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
strtk::type_to_string(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
strtk::type_to_string(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
strtk::type_to_string(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
strtk::type_to_string(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
strtk::type_to_string(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
namespace so
{
void itostr(int n, std::string& s)
{
if (n == 0)
{
s = "0";
}
int sign = -(n < 0);
unsigned int val = (n ^ sign) - sign;
int size;
if (val >= 10000)
{
if (val >= 10000000)
{
if (val >= 1000000000)
size = 10;
else if (val >= 100000000)
size = 9;
else
size = 8;
}
else
{
if (val >= 1000000)
size = 7;
else if (val >= 100000)
size = 6;
else
size = 5;
}
}
else
{
if (val >= 100)
{
if (val >= 1000)
size = 4;
else
size = 3;
}
else
{
if (val>=10)
size = 2;
else
size = 1;
}
}
s.resize(-sign + size);
char* c = &s[0];
if (sign)
*c++='-';
char* d = c + size - 1;
while (val > 0)
{
*d-- = '0' + (val % 10);
val /= 10;
}
}
}
void so_lexical_cast_test_i2s()
{
print_mode("so");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
so::itostr(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
so::itostr(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
so::itostr(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
so::itostr(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
so::itostr(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
const char digit_pairs[201] = {
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899"
};
static const int BUFFER_SIZE = 11;
void timo_itostr(int val, std::string& out)
{
char buf[BUFFER_SIZE];
char *it = &buf[BUFFER_SIZE - 2];
if (val >= 0)
{
int div = val / 100;
while (div)
{
memcpy(it,&digit_pairs[2 * (val - div * 100)],2);
val = div;
it -= 2;
div = val / 100;
}
memcpy(it,&digit_pairs[2 * val],2);
if (val < 10)
it++;
}
else
{
int div = val / 100;
while (div)
{
memcpy(it,&digit_pairs[-2 * (val - div * 100)],2);
val = div;
it -= 2;
div = val / 100;
}
memcpy(it,&digit_pairs[-2 * val],2);
if (val <= -10)
it--;
*it = '-';
}
out.assign(it,&buf[BUFFER_SIZE] - it);
}
void timo_lexical_cast_test_i2s()
{
print_mode("timo");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
timo_itostr(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
timo_itostr(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
timo_itostr(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
timo_itostr(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
timo_itostr(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
namespace voigt
{
template<typename T>
struct assert_integral
{
enum { value = (T)0.5 };
char test[1 - 4 * value];
};
template<typename T, bool is_signed, size_t bits>
struct itostr_impl { };
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,8>
{
static std::string cvt(T val)
{
std::string retval(5, '\0');
int i = 0;
char ch = 0;
if (is_signed)
{
if (val < 0)
{
retval[i] = '-';
++i;
if (val <= -100)
{
ch = '1';
val += 100;
}
val = -val;
}
else if (val >= 100)
{
ch |= '1';
val -= 100;
}
}
else
{
if (val >= 200)
{
ch |= '2';
val -= 200;
}
else if (val >= 100)
{
ch |= '1';
val -= 100;
}
}
if (ch)
{
retval[i] = ch;
++i;
ch = '0';
}
if (val >= 80)
{
ch |= '8';
val -= 80;
}
else if (val >= 40)
{
ch |= '4';
val -= 40;
}
if (val >= 20)
{
ch |= '2';
val -= 20;
}
if (val >= 10)
{
ch |= '1';
val -= 10;
}
if (ch)
{
retval[i] = ch;
++i;
}
retval[i] = '0' + val;
retval.resize(i+1);
return retval;
}
};
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,16>
{
static std::string cvt(T val)
{
std::string retval(7, '\0');
int i = 0;
char ch = 0;
if (is_signed)
{
if (val < 0)
{
retval[i] = '-';
++i;
if (val <= -20000)
{
ch = '2';
val += 20000;
}
val = -val;
}
else if (val >= 20000)
{
ch |= '2';
val -= 20000;
}
}
else
{
if (val >= 40000)
{
ch |= '4';
val -= 40000;
}
else if (val >= 20000)
{
ch |= '2';
val -= 20000;
}
}
if (val >= 10000)
{
ch |= '1';
val -= 10000;
}
if (ch)
{
retval[i] = ch;
++i;
ch = '0';
}
if (val >= 8000)
{
ch |= '8';
val -= 8000;
}
else if (val >= 4000)
{
ch |= '4';
val -= 4000;
}
if (val >= 2000)
{
ch |= '2';
val -= 2000;
}
if (val >= 1000)
{
ch |= '1';
val -= 1000;
}
if (ch)
{
retval[i] = ch;
++i;
ch = '0';
}
if (val >= 800)
{
ch |= '8';
val -= 800;
}
else if (val >= 400)
{
ch |= '4';
val -= 400;
}
if (val >= 200)
{
ch |= '2';
val -= 200;
}
if (val >= 100)
{
ch |= '1';
val -= 100;
}
if (ch)
{
retval[i] = ch;
++i;
ch = '0';
}
if (val >= 80)
{
ch |= '8';
val -= 80;
}
else if (val >= 40)
{
ch |= '4';
val -= 40;
}
if (val >= 20)
{
ch |= '2';
val -= 20;
}
if (val >= 10)
{
ch |= '1';
val -= 10;
}
if (ch)
{
retval[i] = ch;
++i;
}
retval[i] = '0' + val;
retval.resize(i+1);
return retval;
}
};
const char digit_pair_table[201] = {
"00010203040506070809"
"10111213141516171819"
"20212223242526272829"
"30313233343536373839"
"40414243444546474849"
"50515253545556575859"
"60616263646566676869"
"70717273747576777879"
"80818283848586878889"
"90919293949596979899"
};
template<typename T, bool is_signed>
struct itostr_impl<T,is_signed,32>
{
static std::string cvt(T val)
{
char buf[11], ch = 0;
char* start = buf + 1;
char* p = start;
bool neg = val < 0;
int digit;
if (is_signed)
{
if (neg)
{
if (val <= -2000000000)
{
ch = '2';
val += 2000000000;
}
val = -val;
}
else if (val >= 2000000000)
{
ch = '2';
val -= 2000000000;
}
}
else
{
if (val >= 4000000000U)
{
ch |= '4';
val -= 4000000000U;
}
else if (val >= 2000000000)
{
ch |= '2';
val -= 2000000000;
}
}
if (val >= 1000000000)
{
ch |= '1';
val -= 1000000000;
}
if (ch)
{
*p = ch;
++p;
ch = '0';
}
else if (val < 1000)
{
if (val < 10) goto d1;
if (val < 1000) goto d10;
}
else
{
if (val < 100000) goto d1000;
if (val < 10000000) goto d100000;
}
#define DO_PAIR(n) \
d##n: \
digit = val / n; \
*(p++) = digit_pair_table[digit * 2]; \
*(p++) = digit_pair_table[digit * 2 + 1]; \
val -= n * digit;
DO_PAIR(10000000);
DO_PAIR(100000);
DO_PAIR(1000);
DO_PAIR(10);
d1:
*p = '0' | static_cast<char>(val);
if (p > start && *start == '0') ++start;
if (is_signed && neg) *--start = '-';
return std::string(start, p + 1 - start);
}
};
template<typename T>
std::string itostr(T val)
{
//sizeof(assert_integral<T>);
return itostr_impl<T,((T)-1) < 0,sizeof(T) * CHAR_BIT>::cvt(val);
}
}
void voigt_lexical_cast_test_i2s()
{
print_mode("voigt");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
s = voigt::itostr(((i & 1) ? i : -i));
total_length += s.size();
#endif
#ifdef enable_test_type02
s = voigt::itostr(max_i2s + i);
total_length += s.size();
#endif
#ifdef enable_test_type03
s = voigt::itostr(randval[(max_i2s + i) & 1023]);
total_length += s.size();
#endif
#ifdef enable_test_type04
s = voigt::itostr(rand_large_val[(max_i2s + i) & 511]);
total_length += s.size();
#endif
#ifdef enable_test_type05
s = voigt::itostr(rand_large_val[(max_i2s + i) & 31]);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
namespace hopman_fast
{
struct itostr_helper
{
static unsigned out[10000];
itostr_helper()
{
for (int i = 0; i < 10000; i++)
{
unsigned v = i;
char * o = (char*)(out + i);
o[3] = v % 10 + '0';
o[2] = (v % 100) / 10 + '0';
o[1] = static_cast<char>((v % 1000) / 100) + '0';
o[0] = static_cast<char>((v % 10000) / 1000);
if (o[0]) o[0] |= 0x30;
else if (o[1] != '0') o[0] |= 0x20;
else if (o[2] != '0') o[0] |= 0x10;
else o[0] |= 0x00;
}
}
};
unsigned itostr_helper::out[10000];
itostr_helper hlp_init;
template <typename T>
void itostr(T o, std::string& out)
{
typedef itostr_helper hlp;
unsigned blocks[3], *b = blocks + 2;
blocks[0] = o < 0 ? ~o + 1 : o;
blocks[2] = blocks[0] % 10000; blocks[0] /= 10000;
blocks[2] = hlp::out[blocks[2]];
if (blocks[0])
{
blocks[1] = blocks[0] % 10000; blocks[0] /= 10000;
blocks[1] = hlp::out[blocks[1]];
blocks[2] |= 0x30303030;
b--;
}
if (blocks[0])
{
blocks[0] = hlp::out[blocks[0] % 10000];
blocks[1] |= 0x30303030;
b--;
}
char* f = ((char*)b);
f += 3 - (*f >> 4);
char* str = (char*)blocks;
if (o < 0) *--f = '-';
out.assign(f, (str + 12) - f);
}
template <typename T>
std::string itostr(T o)
{
std::string result;
itostr(o,result);
return result;
}
}
void hopman_fast_lexical_cast_test_i2s()
{
print_mode("hopman");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
hopman_fast::itostr(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
hopman_fast::itostr(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
hopman_fast::itostr(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
hopman_fast::itostr(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
hopman_fast::itostr(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
void zverovich_lexical_cast_test_i2s()
{
print_mode("zverovich");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
s = fmt::FormatInt(((i & 1) ? i : -i)).c_str();
total_length += s.size();
#endif
#ifdef enable_test_type02
s = fmt::FormatInt(max_i2s + i).c_str();
total_length += s.size();
#endif
#ifdef enable_test_type03
s = fmt::FormatInt(randval[(max_i2s + i) & 1023]).c_str();
total_length += s.size();
#endif
#ifdef enable_test_type04
s = fmt::FormatInt(rand_large_val[(max_i2s + i) & 511]).c_str();
total_length += s.size();
#endif
#ifdef enable_test_type05
s = fmt::FormatInt(edgeval[(max_i2s + i) & 31]).c_str();
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
namespace jiaendu
{
inline int ufast_utoa10(unsigned int value, char* str)
{
#ifndef int32_t
#define int32_t int
#define uint32_t unsigned int
#define int16_t short
#define int64_t long long
#define uint64_t unsigned long long
#define jiaendudefined_types
#endif
#define JOIN(N) \
N "0", N "1", N "2", N "3", N "4", N "5", N "6", N "7", N "8", N "9" \
#define JOIN2(N) \
JOIN(N "0"), JOIN(N "1"), JOIN(N "2"), JOIN(N "3"), JOIN(N "4"), \
JOIN(N "5"), JOIN(N "6"), JOIN(N "7"), JOIN(N "8"), JOIN(N "9") \
#define JOIN3(N) \
JOIN2(N "0"), JOIN2(N "1"), JOIN2(N "2"), JOIN2(N "3"), JOIN2(N "4"), \
JOIN2(N "5"), JOIN2(N "6"), JOIN2(N "7"), JOIN2(N "8"), JOIN2(N "9") \
#define JOIN4 \
JOIN3("0"), JOIN3("1"), JOIN3("2"), JOIN3("3"), JOIN3("4"), \
JOIN3("5"), JOIN3("6"), JOIN3("7"), JOIN3("8"), JOIN3("9") \
#define JOIN5(N) \
JOIN(N), JOIN(N "1"), JOIN(N "2"), JOIN(N "3"), JOIN(N "4"), \
JOIN(N "5"), JOIN(N "6"), JOIN(N "7"), JOIN(N "8"), JOIN(N "9") \
#define JOIN6 \
JOIN5(""), JOIN2("1"), JOIN2("2"), JOIN2("3"), JOIN2("4"), \
JOIN2("5"), JOIN2("6"), JOIN2("7"), JOIN2("8"), JOIN2("9") \
#define F(N) ((N) >= 100 ? 3 : (N) >= 10 ? 2 : 1)
#define F10(N) \
F(N), F(N + 1), F(N + 2), F(N + 3), F(N + 4), \
F(N + 5), F(N + 6), F(N + 7), F(N + 8), F(N + 9) \
#define F100(N) \
F10(N), F10(N + 10), F10(N + 20), F10(N + 30), \
F10(N + 40), F10(N + 50), F10(N + 60), F10(N + 70),\
F10(N + 80), F10(N + 90) \
static const short offsets[] = {
F100( 0), F100(100), F100(200), F100(300), F100(400),
F100(500), F100(600), F100(700), F100(800), F100(900)
};
static const char table1[][4] = { JOIN ("") };
static const char table2[][4] = { JOIN2("") };
static const char table3[][4] = { JOIN3("") };
static const char table4[][8] = { JOIN4 };
static const char table5[][4] = { JOIN6 };
#undef JOIN
#undef JOIN2
#undef JOIN3
#undef JOIN4
#undef F
#undef F10
#undef F100
char *wstr;
#if (_WIN64 || __x86_64__ || __ppc64__)
uint64_t remains[2];
#else
uint32_t remains[2];
#endif
unsigned int v2;
if (value >= 100000000)
{
#if (_WIN64 || __x86_64__ || __ppc64__)
remains[0] = (((uint64_t)value * (uint64_t)3518437209) >> 45);
remains[1] = (((uint64_t)value * (uint64_t)2882303762) >> 58);
#else
remains[0] = value / 10000;
remains[1] = value / 100000000;
#endif
v2 = remains[1];
remains[1] = remains[0] - remains[1] * 10000;
remains[0] = value - remains[0] * 10000;
if (v2 >= 10)
{
memcpy(str,table5[v2],2);
str += 2;
memcpy(str,table4[remains[1]],4);
str += 4;
memcpy(str,table4[remains[0]],4);
return 10;
}
else
{
*(char *) str = v2 + '0';
str += 1;
memcpy(str,table4[remains[1]],4);
str += 4;
memcpy(str,table4[remains[0]],4);
return 9;
}
}
else if (value >= 10000)
{
#if (_WIN64 || __x86_64__ || __ppc64__)
v2 = (((uint64_t)value * (uint64_t)3518437209 ) >> 45);
#else
v2 = value / 10000;
#endif
remains[0] = value - v2 * 10000;
if (v2 >= 1000)
{
memcpy(str,table4[v2],4);
str += 4;
memcpy(str,table4[remains[0]],4);
return 8;
}
else
{
wstr = str;
memcpy(wstr,table5[v2],4);
wstr += offsets[v2];
memcpy(wstr,table4[remains[0]],4);
wstr += 4;
return (wstr - str);
}
}
else
{
if (value >= 1000)
{
memcpy(str,table4[value],4);
return 4;
}
else if (value >= 100)
{
memcpy(str,table3[value],3);
return 3;
}
else if (value >= 10)
{
memcpy(str,table2[value],2);
return 2;
}
else
{
*(char *) str = *(char *) table1[value];
return 1;
}
}
}
void ufast_itoa10v2(int value, std::string &s)
{
char buf[32];
char* str = buf;
std::size_t size;
if (value < 0)
{
*(str++) = '-';
size = ufast_utoa10(-value, str) + 1;
}
else
size = ufast_utoa10(value, str);
s.assign(buf,size);
}
}
void jiaendu_lexical_cast_test_i2s()
{
print_mode("jiaendu");
std::string s;
s.reserve(32);
std::size_t total_length = 0;
strtk::util::timer t;
t.start();
for (int i = (-max_i2s / 2); i < (max_i2s / 2); ++i)
{
#ifdef enable_test_type01
jiaendu::ufast_itoa10v2(((i & 1) ? i : -i),s);
total_length += s.size();
#endif
#ifdef enable_test_type02
jiaendu::ufast_itoa10v2(max_i2s + i,s);
total_length += s.size();
#endif
#ifdef enable_test_type03
jiaendu::ufast_itoa10v2(randval[(max_i2s + i) & 1023],s);
total_length += s.size();
#endif
#ifdef enable_test_type04
jiaendu::ufast_itoa10v2(rand_large_val[(max_i2s + i) & 511],s);
total_length += s.size();
#endif
#ifdef enable_test_type05
jiaendu::ufast_itoa10v2(edgeval[(max_i2s + i) & 31],s);
total_length += s.size();
#endif
}
t.stop();
printf("Numbers:%10lu\tTotal:%12lu\tTime:%8.4fsec\tRate:%14.4fnums/sec\n",
static_cast<unsigned long>(test_multiplier() * max_i2s),
static_cast<unsigned long>(total_length),
t.time(),
(test_multiplier() * max_i2s) / t.time());
}
void validation_check()
{
const int v[] =
{
0,1,2,3,4,5,6,7,8,9,
11,33,55,77,99,
111,333,555,777,999,
1111,3333,5555,7777,9999,
11111,33333,55555,77777,99999,
111111,333333,555555,777777,999999,
1111111,3333333,5555555,7777777,9999999,
11111111,33333333,55555555,77777777,99999999,
111111111,333333333,555555555,777777777,999999999,
-1, -2, -3, -4, -5, -6, -7, -8, -9,
-11, -33, -55, -77, -99,
-111, -333, -555, -777, -999,
-1111, -3333, -5555, -7777, -9999,
-11111, -33333, -55555, -77777, -99999,
-111111, -333333, -555555, -777777, -999999,
-1111111, -3333333, -5555555, -7777777, -9999999,
-11111111, -33333333, -55555555, -77777777, -99999999,
-111111111, -333333333, -555555555, -777777777, -999999999,
2147483647,-2147483648
};
const std::size_t v_size = sizeof(v) / sizeof(int);
const std::string s[] =
{
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"11", "33", "55", "77", "99",
"111", "333", "555", "777", "999",
"1111", "3333", "5555", "7777", "9999",
"11111", "33333", "55555", "77777", "99999",
"111111", "333333", "555555", "777777", "999999",
"1111111", "3333333", "5555555", "7777777", "9999999",
"11111111", "33333333", "55555555", "77777777", "99999999",
"111111111", "333333333", "555555555", "777777777", "999999999",
"-1", "-2", "-3", "-4", "-5", "-6", "-7", "-8", "-9",
"-11", "-33", "-55", "-77", "-99",
"-111", "-333", "-555", "-777", "-999",
"-1111", "-3333", "-5555", "-7777", "-9999",
"-11111", "-33333", "-55555", "-77777", "-99999",
"-111111", "-333333", "-555555", "-777777", "-999999",
"-1111111", "-3333333", "-5555555", "-7777777", "-9999999",
"-11111111", "-33333333", "-55555555", "-77777777", "-99999999",
"-111111111", "-333333333", "-555555555", "-777777777", "-999999999",
"2147483647", "-2147483648"
};
for (std::size_t i = 0; i < v_size; ++i)
{
std::string result;
result.clear();
result = boost::lexical_cast<std::string>(v[i]);
if (result != s[i]) { printf("boost - Failed: %d\n",v[i]); }
result.clear();
karma_int_to_string(v[i],result);
if (result != s[i]) { printf("karma - Failed: %d\n",v[i]); }
result.clear();
strtk::type_to_string(v[i],result);
if (result != s[i]) { printf("strtk - Failed: %d\n",v[i]); }
result.clear();
so::itostr(v[i],result);
if (result != s[i]) { printf("so - Failed: %d\n",v[i]); }
result.clear();
timo_itostr(v[i],result);
if (result != s[i]) { printf("timo - Failed: %d\n",v[i]); }
result.clear();
result = voigt::itostr(v[i]);
if (result != s[i]) { printf("voigt - Failed: %d\n",v[i]); }
result.clear();
hopman_fast::itostr(v[i],result);
if (result != s[i]) { printf("hopman - Failed: %d\n",v[i]); }
result.clear();
result = fmt::FormatInt(v[i]).c_str();
if (result != s[i]) { printf("zverovich - Failed: %d\n",v[i]); }
result.clear();
jiaendu::ufast_itoa10v2(v[i],result);
if (result != s[i]) { printf("jiaendu - Failed: %d\n",v[i]); }
}
}
int main()
{
std::cout << "Integer To String Test" << std::endl;
validation_check();
flush_cache();
sprintf_lexical_cast_test_i2s();
//flush_cache();
//boost_lexical_cast_test_i2s(); too slow to even be considered.
flush_cache();
karma_lexical_cast_test_i2s();
flush_cache();
strtk_lexical_cast_test_i2s();
flush_cache();
so_lexical_cast_test_i2s();
flush_cache();
timo_lexical_cast_test_i2s();
flush_cache();
voigt_lexical_cast_test_i2s();
flush_cache();
hopman_fast_lexical_cast_test_i2s();
flush_cache();
zverovich_lexical_cast_test_i2s();
flush_cache();
jiaendu_lexical_cast_test_i2s();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment