项目作者: rigtorp

项目描述 :
Fast integer to string and string to integer conversion functions
高级语言: C++
项目地址: git://github.com/rigtorp/CharConv.git
创建时间: 2017-12-26T21:24:44Z
项目社区:https://github.com/rigtorp/CharConv

开源协议:MIT License

下载


CharConv.h

License

Fast integer to string and string to integer conversion functions.

Usage

CharConv.h exposes two functions: to_chars and from_chars. The API is
similar, but not identical to that of
to_chars and
from_chars in the C++17
standard.

  • to_chars_result to_chars(char *first, char *last, int32_t value) noexcept
  • to_chars_result to_chars(char *first, char *last, uint32_t value) noexcept
  • to_chars_result to_chars(char *first, char *last, int64_t value) noexcept
  • to_chars_result to_chars(char *first, char *last, uint64_t value) noexcept

    Converts value into characters in base 10.

  • from_chars_result from_chars(const char *first, const char *last, int32_t &value) noexcept

  • from_chars_result from_chars(const char *first, const char *last, uint32_t &value) noexcept
  • from_chars_result from_chars(const char *first, const char *last, int64_t &value) noexcept
  • from_chars_result from_chars(const char *first, const char *last, uint64_t &value) noexcept

    Converts the character sequence [first, last] representing
    a base 10 number to an integer value.

Example

  1. using namespace rigtorp;
  2. std::array<char, 32> buf = {};
  3. to_chars(buf.begin(), buf.end(), 123));
  4. std::cout << buf.data();
  5. int result;
  6. from_chars(buf.begin(), buf.end(), result);
  7. std::cout << result;

Benchmark

Running on Intel(R) Xeon(R) CPU E5-2620 v4 @ 2.10GHz:

  1. 2017-12-26 15:08:09
  2. Run on (32 X 3000 MHz CPU s)
  3. CPU Caches:
  4. L1 Data 32K (x16)
  5. L1 Instruction 32K (x16)
  6. L2 Unified 256K (x16)
  7. L3 Unified 20480K (x2)
  8. -----------------------------------------------------------------
  9. Benchmark Time CPU Iterations
  10. -----------------------------------------------------------------
  11. BM_sprintf/1 73 ns 73 ns 9625985
  12. BM_sprintf/2 75 ns 75 ns 9284410
  13. BM_sprintf/3 77 ns 77 ns 9135939
  14. BM_sprintf/4 79 ns 79 ns 8868328
  15. BM_sprintf/5 80 ns 80 ns 8716034
  16. BM_sprintf/6 82 ns 82 ns 8561608
  17. BM_sprintf/7 83 ns 83 ns 8426476
  18. BM_sprintf/8 85 ns 85 ns 8219398
  19. BM_sprintf/9 87 ns 87 ns 8016940
  20. BM_to_string/1 76 ns 76 ns 9159664
  21. BM_to_string/2 79 ns 79 ns 8852605
  22. BM_to_string/3 81 ns 81 ns 8612168
  23. BM_to_string/4 83 ns 83 ns 8388007
  24. BM_to_string/5 85 ns 85 ns 8289128
  25. BM_to_string/6 86 ns 86 ns 8124345
  26. BM_to_string/7 87 ns 87 ns 8049160
  27. BM_to_string/8 89 ns 89 ns 7870350
  28. BM_to_string/9 90 ns 90 ns 7744079
  29. BM_stringstream/1 34 ns 34 ns 20586984
  30. BM_stringstream/2 38 ns 38 ns 18778128
  31. BM_stringstream/3 39 ns 39 ns 17165452
  32. BM_stringstream/4 43 ns 43 ns 16696859
  33. BM_stringstream/5 45 ns 45 ns 16127302
  34. BM_stringstream/6 47 ns 47 ns 15600041
  35. BM_stringstream/7 51 ns 51 ns 10000000
  36. BM_stringstream/8 51 ns 51 ns 13379508
  37. BM_stringstream/9 55 ns 55 ns 12981355
  38. BM_to_chars_naive/1 12 ns 12 ns 58830826
  39. BM_to_chars_naive/2 14 ns 14 ns 51283290
  40. BM_to_chars_naive/3 15 ns 15 ns 47083964
  41. BM_to_chars_naive/4 16 ns 16 ns 43907170
  42. BM_to_chars_naive/5 17 ns 17 ns 41087032
  43. BM_to_chars_naive/6 18 ns 18 ns 38062908
  44. BM_to_chars_naive/7 19 ns 19 ns 36324383
  45. BM_to_chars_naive/8 21 ns 21 ns 33114607
  46. BM_to_chars_naive/9 22 ns 22 ns 31397235
  47. BM_to_chars/1 9 ns 9 ns 77857609
  48. BM_to_chars/2 10 ns 10 ns 71753282
  49. BM_to_chars/3 11 ns 11 ns 54759461
  50. BM_to_chars/4 12 ns 12 ns 56404502
  51. BM_to_chars/5 14 ns 14 ns 50743796
  52. BM_to_chars/6 15 ns 15 ns 47294406
  53. BM_to_chars/7 16 ns 16 ns 43201370
  54. BM_to_chars/8 18 ns 18 ns 39207877
  55. BM_to_chars/9 20 ns 20 ns 35792902
  56. BM_atoi/1 14 ns 14 ns 48904147
  57. BM_atoi/2 16 ns 16 ns 42837308
  58. BM_atoi/3 18 ns 18 ns 39150309
  59. BM_atoi/4 20 ns 20 ns 35951644
  60. BM_atoi/5 21 ns 21 ns 33153980
  61. BM_atoi/6 23 ns 23 ns 30722978
  62. BM_atoi/7 24 ns 24 ns 28734136
  63. BM_atoi/8 26 ns 26 ns 26651732
  64. BM_atoi/9 28 ns 28 ns 25124905
  65. BM_strtol/1 14 ns 15 ns 48394136
  66. BM_strtol/2 16 ns 16 ns 42688678
  67. BM_strtol/3 18 ns 18 ns 39113890
  68. BM_strtol/4 20 ns 20 ns 35940516
  69. BM_strtol/5 21 ns 21 ns 33045007
  70. BM_strtol/6 23 ns 23 ns 30704735
  71. BM_strtol/7 24 ns 24 ns 28600808
  72. BM_strtol/8 26 ns 26 ns 26730526
  73. BM_strtol/9 28 ns 28 ns 25050583
  74. BM_stoi/1 16 ns 16 ns 44890941
  75. BM_stoi/2 17 ns 17 ns 40451418
  76. BM_stoi/3 19 ns 19 ns 37593651
  77. BM_stoi/4 20 ns 20 ns 34442293
  78. BM_stoi/5 22 ns 22 ns 31927689
  79. BM_stoi/6 24 ns 24 ns 29465662
  80. BM_stoi/7 25 ns 25 ns 27752163
  81. BM_stoi/8 27 ns 27 ns 25848348
  82. BM_stoi/9 29 ns 29 ns 24381987
  83. BM_from_chars_unchecked/1 4 ns 4 ns 162408633
  84. BM_from_chars_unchecked/2 5 ns 5 ns 132550992
  85. BM_from_chars_unchecked/3 6 ns 6 ns 113085260
  86. BM_from_chars_unchecked/4 7 ns 7 ns 96808997
  87. BM_from_chars_unchecked/5 8 ns 8 ns 89169648
  88. BM_from_chars_unchecked/6 8 ns 8 ns 82540712
  89. BM_from_chars_unchecked/7 9 ns 9 ns 77455690
  90. BM_from_chars_unchecked/8 10 ns 10 ns 73136554
  91. BM_from_chars_unchecked/9 10 ns 10 ns 68730597
  92. BM_from_chars/1 9 ns 9 ns 78818376
  93. BM_from_chars/2 10 ns 10 ns 71176282
  94. BM_from_chars/3 11 ns 11 ns 63257070
  95. BM_from_chars/4 12 ns 12 ns 56995175
  96. BM_from_chars/5 14 ns 14 ns 50584861
  97. BM_from_chars/6 15 ns 15 ns 47135520
  98. BM_from_chars/7 16 ns 16 ns 43457090
  99. BM_from_chars/8 17 ns 17 ns 40382499
  100. BM_from_chars/9 19 ns 19 ns 37375537

About

This project was created by Erik Rigtorp
[erik@rigtorp.se](mailto:erik@rigtorp.se).