|
46 | 46 |
|
47 | 47 | - [General](#general) |
48 | 48 | - [Array](#array) |
| 49 | +- [BigDecimal](#bigdecimal) |
49 | 50 | - [Date](#date) |
50 | 51 | - [Enumerable](#enumerable) |
51 | 52 | - [Hash](#hash) |
@@ -881,6 +882,165 @@ Comparison: |
881 | 882 | Array#inject(:+): 977701.4 i/s - 1.56x slower |
882 | 883 | ``` |
883 | 884 |
|
| 885 | +### BigDecimal |
| 886 | + |
| 887 | +##### `BigDecimal` from a number vs from a String [integer code](code/bigdecimal/integer-string-vs-numeric.rb) [float code](code/bigdecimal/float-string-vs-numeric.rb) |
| 888 | + |
| 889 | +On current CRuby, building a `BigDecimal` from the number is faster than from a String. |
| 890 | +On Ruby 2.5 it was the other way around, as the 2.5 runs below show. |
| 891 | + |
| 892 | +The first run is from #172, when both files were one. |
| 893 | + |
| 894 | +``` |
| 895 | +$ ruby -v code/bigdecimal/string-vs-numeric.rb |
| 896 | +ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-darwin18] |
| 897 | +Calculating ------------------------------------- |
| 898 | + integer string new 2.497M (± 2.0%) i/s - 12.645M in 5.066385s |
| 899 | + float string new 2.414M (± 2.0%) i/s - 12.182M in 5.048473s |
| 900 | + integer string to_d 2.402M (± 1.8%) i/s - 12.010M in 5.001784s |
| 901 | + float string to_d 2.318M (± 1.8%) i/s - 11.663M in 5.032429s |
| 902 | + integer new 1.601M (± 1.2%) i/s - 8.022M in 5.010201s |
| 903 | + integer to_d 1.563M (± 1.8%) i/s - 7.817M in 5.001726s |
| 904 | + float new 517.635k (± 4.5%) i/s - 2.604M in 5.042605s |
| 905 | + float to_d 529.413k (± 3.6%) i/s - 2.671M in 5.051477s |
| 906 | +
|
| 907 | +Comparison: |
| 908 | + integer string new: 2496860.3 i/s |
| 909 | + float string new: 2414122.8 i/s - same-ish: difference falls within error |
| 910 | + integer string to_d: 2401929.3 i/s - 1.04x slower |
| 911 | + float string to_d: 2318272.7 i/s - 1.08x slower |
| 912 | + integer new: 1601402.4 i/s - 1.56x slower |
| 913 | + integer to_d: 1563335.6 i/s - 1.60x slower |
| 914 | + float to_d: 529412.7 i/s - 4.72x slower |
| 915 | + float new: 517634.5 i/s - 4.82x slower |
| 916 | +``` |
| 917 | + |
| 918 | +``` |
| 919 | +$ ruby -v code/bigdecimal/integer-string-vs-numeric.rb |
| 920 | +ruby 2.5.9p229 (2021-04-05 revision 67939) [aarch64-linux] |
| 921 | +Warming up -------------------------------------- |
| 922 | + integer new 414.193k i/100ms |
| 923 | + integer to_d 403.499k i/100ms |
| 924 | + integer string new 550.222k i/100ms |
| 925 | + integer string to_d 495.285k i/100ms |
| 926 | +Calculating ------------------------------------- |
| 927 | + integer new 4.270M (± 4.1%) i/s (234.17 ns/i) - 21.538M in 5.043465s |
| 928 | + integer to_d 3.937M (± 6.7%) i/s (254.03 ns/i) - 19.771M in 5.022536s |
| 929 | + integer string new 5.491M (± 1.7%) i/s (182.11 ns/i) - 27.511M in 5.010070s |
| 930 | + integer string to_d 4.985M (± 0.5%) i/s (200.61 ns/i) - 25.260M in 5.067219s |
| 931 | +
|
| 932 | +Comparison: |
| 933 | + integer string new: 5491160.4 i/s |
| 934 | +integer string to_d: 4984891.5 i/s - 1.10x slower |
| 935 | + integer new: 4270484.2 i/s - 1.29x slower |
| 936 | + integer to_d: 3936547.2 i/s - 1.39x slower |
| 937 | +``` |
| 938 | + |
| 939 | +``` |
| 940 | +$ ruby -v code/bigdecimal/integer-string-vs-numeric.rb |
| 941 | +ruby 3.4.10 (2026-06-30 revision 2b0b7728dc) +PRISM [aarch64-linux] |
| 942 | +Warming up -------------------------------------- |
| 943 | + integer new 724.240k i/100ms |
| 944 | + integer to_d 669.146k i/100ms |
| 945 | + integer string new 476.320k i/100ms |
| 946 | + integer string to_d 473.860k i/100ms |
| 947 | +Calculating ------------------------------------- |
| 948 | + integer new 8.201M (± 8.4%) i/s (121.94 ns/i) - 41.282M in 5.033937s |
| 949 | + integer to_d 7.789M (± 1.9%) i/s (128.39 ns/i) - 39.480M in 5.068714s |
| 950 | + integer string new 4.675M (± 6.1%) i/s (213.88 ns/i) - 23.816M in 5.093797s |
| 951 | + integer string to_d 4.537M (± 9.6%) i/s (220.43 ns/i) - 22.745M in 5.013831s |
| 952 | +
|
| 953 | +Comparison: |
| 954 | + integer new: 8200675.5 i/s |
| 955 | + integer to_d: 7788881.6 i/s - same-ish: difference falls within error |
| 956 | + integer string new: 4675490.6 i/s - 1.75x slower |
| 957 | +integer string to_d: 4536507.4 i/s - 1.81x slower |
| 958 | +``` |
| 959 | + |
| 960 | +``` |
| 961 | +$ ruby -v code/bigdecimal/integer-string-vs-numeric.rb |
| 962 | +ruby 4.0.7 (2026-09-15 revision 229531a6cf) +PRISM [aarch64-linux] |
| 963 | +Warming up -------------------------------------- |
| 964 | + integer new 833.601k i/100ms |
| 965 | + integer to_d 998.642k i/100ms |
| 966 | + integer string new 612.931k i/100ms |
| 967 | + integer string to_d 622.024k i/100ms |
| 968 | +Calculating ------------------------------------- |
| 969 | + integer new 11.461M (± 8.4%) i/s (87.25 ns/i) - 57.518M in 5.018754s |
| 970 | + integer to_d 9.982M (± 7.9%) i/s (100.18 ns/i) - 49.932M in 5.002014s |
| 971 | + integer string new 6.318M (± 0.8%) i/s (158.29 ns/i) - 31.872M in 5.045082s |
| 972 | + integer string to_d 6.048M (± 7.3%) i/s (165.34 ns/i) - 30.479M in 5.039416s |
| 973 | +
|
| 974 | +Comparison: |
| 975 | + integer new: 11460706.3 i/s |
| 976 | + integer to_d: 9982400.0 i/s - same-ish: difference falls within error |
| 977 | + integer string new: 6317520.9 i/s - 1.81x slower |
| 978 | +integer string to_d: 6048156.9 i/s - 1.89x slower |
| 979 | +``` |
| 980 | + |
| 981 | +``` |
| 982 | +$ ruby -v code/bigdecimal/float-string-vs-numeric.rb |
| 983 | +ruby 2.5.9p229 (2021-04-05 revision 67939) [aarch64-linux] |
| 984 | +Warming up -------------------------------------- |
| 985 | + float new 156.784k i/100ms |
| 986 | + float to_d 140.804k i/100ms |
| 987 | + float string new 544.740k i/100ms |
| 988 | + float string to_d 489.796k i/100ms |
| 989 | +Calculating ------------------------------------- |
| 990 | + float new 1.571M (± 1.7%) i/s (636.41 ns/i) - 7.996M in 5.088751s |
| 991 | + float to_d 1.443M (± 1.3%) i/s (693.24 ns/i) - 7.322M in 5.075776s |
| 992 | + float string new 5.430M (± 0.9%) i/s (184.16 ns/i) - 27.237M in 5.016063s |
| 993 | + float string to_d 4.714M (± 8.8%) i/s (212.13 ns/i) - 24.000M in 5.091113s |
| 994 | +
|
| 995 | +Comparison: |
| 996 | + float string new: 5429956.2 i/s |
| 997 | +float string to_d: 4714097.3 i/s - 1.15x slower |
| 998 | + float new: 1571305.9 i/s - 3.46x slower |
| 999 | + float to_d: 1442500.1 i/s - 3.76x slower |
| 1000 | +``` |
| 1001 | + |
| 1002 | +``` |
| 1003 | +$ ruby -v code/bigdecimal/float-string-vs-numeric.rb |
| 1004 | +ruby 3.4.10 (2026-06-30 revision 2b0b7728dc) +PRISM [aarch64-linux] |
| 1005 | +Warming up -------------------------------------- |
| 1006 | + float new 562.944k i/100ms |
| 1007 | + float to_d 515.554k i/100ms |
| 1008 | + float string new 464.840k i/100ms |
| 1009 | + float string to_d 383.190k i/100ms |
| 1010 | +Calculating ------------------------------------- |
| 1011 | + float new 6.390M (± 1.2%) i/s (156.50 ns/i) - 32.088M in 5.021892s |
| 1012 | + float to_d 5.731M (± 2.4%) i/s (174.50 ns/i) - 28.871M in 5.037882s |
| 1013 | + float string new 4.741M (± 2.3%) i/s (210.93 ns/i) - 23.707M in 5.000438s |
| 1014 | + float string to_d 4.826M (± 1.7%) i/s (207.19 ns/i) - 24.141M in 5.001873s |
| 1015 | +
|
| 1016 | +Comparison: |
| 1017 | + float new: 6389585.0 i/s |
| 1018 | + float to_d: 5730786.4 i/s - 1.11x slower |
| 1019 | +float string to_d: 4826386.1 i/s - 1.32x slower |
| 1020 | + float string new: 4740952.5 i/s - 1.35x slower |
| 1021 | +``` |
| 1022 | + |
| 1023 | +``` |
| 1024 | +$ ruby -v code/bigdecimal/float-string-vs-numeric.rb |
| 1025 | +ruby 4.0.7 (2026-09-15 revision 229531a6cf) +PRISM [aarch64-linux] |
| 1026 | +Warming up -------------------------------------- |
| 1027 | + float new 808.735k i/100ms |
| 1028 | + float to_d 710.639k i/100ms |
| 1029 | + float string new 603.015k i/100ms |
| 1030 | + float string to_d 589.930k i/100ms |
| 1031 | +Calculating ------------------------------------- |
| 1032 | + float new 7.953M (± 3.4%) i/s (125.74 ns/i) - 40.437M in 5.084493s |
| 1033 | + float to_d 7.089M (± 7.2%) i/s (141.06 ns/i) - 35.532M in 5.012103s |
| 1034 | + float string new 6.205M (± 2.2%) i/s (161.16 ns/i) - 31.357M in 5.053567s |
| 1035 | + float string to_d 6.022M (± 4.0%) i/s (166.06 ns/i) - 30.676M in 5.093998s |
| 1036 | +
|
| 1037 | +Comparison: |
| 1038 | + float new: 7952955.5 i/s |
| 1039 | + float to_d: 7089229.6 i/s - 1.12x slower |
| 1040 | + float string new: 6204880.6 i/s - 1.28x slower |
| 1041 | +float string to_d: 6022060.1 i/s - 1.32x slower |
| 1042 | +``` |
| 1043 | + |
884 | 1044 | ### Date |
885 | 1045 |
|
886 | 1046 | ##### `Date.iso8601` vs `Date.parse` [code](code/date/iso8601-vs-parse.rb) |
|
0 commit comments