Benchmakrs of different enumeration methods in .NET 6.
BenchmarkDotNet=v0.13.1, OS=Windows 10.0.19042.1348 (20H2/October2020Update) AMD Ryzen 9 5900X, 1 CPU, 24 logical and 12 physical cores .NET SDK=6.0.100 [Host] : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT Job-GFBZAZ : .NET 6.0.0 (6.0.21.52210), X64 RyuJIT
Runtime=.NET 6.0 RunStrategy=Throughput
Method | Count | Mean | Error | StdDev | Median | Gen 0 | Gen 1 | Gen 2 | Allocated |
---|---|---|---|---|---|---|---|---|---|
Yield | 100 | 361.42 ns | 3.580 ns | 3.174 ns | 361.49 ns | 0.0024 | - | - | 40 B |
Array | 100 | 92.44 ns | 1.809 ns | 3.486 ns | 91.25 ns | 0.0253 | - | - | 424 B |
SpanArray | 100 | 89.23 ns | 1.100 ns | 1.029 ns | 89.27 ns | 0.0253 | - | - | 424 B |
ArrayAsEnumerable | 100 | 377.97 ns | 3.549 ns | 3.146 ns | 378.54 ns | 0.0272 | - | - | 456 B |
ArrayFor | 100 | 86.55 ns | 1.724 ns | 1.612 ns | 86.23 ns | 0.0253 | - | - | 424 B |
SpanArrayFor | 100 | 92.28 ns | 1.841 ns | 2.699 ns | 92.51 ns | 0.0253 | - | - | 424 B |
ListPreAllocated | 100 | 232.67 ns | 4.668 ns | 8.297 ns | 229.74 ns | 0.0272 | - | - | 456 B |
ListPreAllocatedFor | 100 | 181.78 ns | 3.664 ns | 8.989 ns | 181.46 ns | 0.0272 | - | - | 456 B |
ListAsEnumerablePreAllocated | 100 | 622.14 ns | 8.741 ns | 8.177 ns | 624.76 ns | 0.0296 | - | - | 496 B |
List | 100 | 345.22 ns | 6.792 ns | 8.085 ns | 345.82 ns | 0.0706 | - | - | 1,184 B |
ListFor | 100 | 305.58 ns | 6.113 ns | 15.449 ns | 301.14 ns | 0.0706 | - | - | 1,184 B |
ListAsEnumerable | 100 | 727.09 ns | 7.563 ns | 7.074 ns | 725.14 ns | 0.0725 | - | - | 1,224 B |
EnumerableWithStructEnumerator | 100 | 470.13 ns | 9.267 ns | 10.672 ns | 466.32 ns | 0.0072 | - | - | 120 B |
EnumerableWithClassEnumerator | 100 | 321.00 ns | 6.426 ns | 6.311 ns | 321.32 ns | 0.0072 | - | - | 120 B |
StructEnumerator | 100 | 29.07 ns | 0.597 ns | 0.613 ns | 29.03 ns | - | - | - | - |
ClassEnumerator | 100 | 49.62 ns | 1.017 ns | 1.522 ns | 48.98 ns | 0.0014 | - | - | 24 B |
Yield | 1000 | 3,573.58 ns | 70.751 ns | 78.640 ns | 3,553.82 ns | - | - | - | 40 B |
Array | 1000 | 829.08 ns | 16.495 ns | 28.889 ns | 827.97 ns | 0.2403 | - | - | 4,024 B |
SpanArray | 1000 | 786.79 ns | 13.776 ns | 16.918 ns | 790.71 ns | 0.2403 | - | - | 4,024 B |
ArrayAsEnumerable | 1000 | 3,693.60 ns | 36.257 ns | 33.915 ns | 3,691.78 ns | 0.2403 | - | - | 4,056 B |
ArrayFor | 1000 | 760.37 ns | 11.362 ns | 17.351 ns | 754.79 ns | 0.2403 | - | - | 4,024 B |
SpanArrayFor | 1000 | 777.42 ns | 14.861 ns | 17.114 ns | 777.95 ns | 0.2403 | - | - | 4,024 B |
ListPreAllocated | 1000 | 2,050.45 ns | 10.798 ns | 9.572 ns | 2,049.04 ns | 0.2422 | 0.0019 | - | 4,056 B |
ListPreAllocatedFor | 1000 | 1,557.72 ns | 18.756 ns | 17.545 ns | 1,550.38 ns | 0.2422 | 0.0019 | - | 4,056 B |
ListAsEnumerablePreAllocated | 1000 | 5,877.41 ns | 89.891 ns | 79.686 ns | 5,881.39 ns | 0.2441 | - | - | 4,096 B |
List | 1000 | 2,442.50 ns | 33.464 ns | 27.944 ns | 2,446.82 ns | 0.4997 | 0.0076 | - | 8,424 B |
ListFor | 1000 | 2,010.49 ns | 39.416 ns | 43.811 ns | 1,999.74 ns | 0.5016 | 0.0095 | - | 8,424 B |
ListAsEnumerable | 1000 | 6,004.08 ns | 63.840 ns | 56.592 ns | 6,013.74 ns | 0.5035 | 0.0076 | - | 8,464 B |
EnumerableWithStructEnumerator | 1000 | 4,087.49 ns | 77.101 ns | 75.723 ns | 4,113.07 ns | - | - | - | 120 B |
EnumerableWithClassEnumerator | 1000 | 3,009.39 ns | 25.946 ns | 24.270 ns | 3,015.38 ns | 0.0038 | - | - | 120 B |
StructEnumerator | 1000 | 230.72 ns | 0.665 ns | 0.520 ns | 230.69 ns | - | - | - | - |
ClassEnumerator | 1000 | 301.13 ns | 4.174 ns | 3.904 ns | 301.03 ns | 0.0014 | - | - | 24 B |
Yield | 10000 | 34,799.27 ns | 655.965 ns | 701.875 ns | 34,825.49 ns | - | - | - | 40 B |
Array | 10000 | 6,968.61 ns | 134.281 ns | 125.606 ns | 6,917.78 ns | 2.3804 | - | - | 40,024 B |
SpanArray | 10000 | 6,919.56 ns | 126.141 ns | 117.992 ns | 6,866.25 ns | 2.3804 | - | - | 40,024 B |
ArrayAsEnumerable | 10000 | 35,334.05 ns | 638.912 ns | 566.379 ns | 35,428.61 ns | 2.3804 | - | - | 40,056 B |
ArrayFor | 10000 | 6,993.88 ns | 136.957 ns | 152.227 ns | 7,005.48 ns | 2.3804 | - | - | 40,024 B |
SpanArrayFor | 10000 | 6,987.85 ns | 105.773 ns | 93.765 ns | 6,969.48 ns | 2.3804 | - | - | 40,024 B |
ListPreAllocated | 10000 | 19,162.08 ns | 172.071 ns | 160.956 ns | 19,191.02 ns | 2.3804 | 0.2747 | - | 40,056 B |
ListPreAllocatedFor | 10000 | 14,484.04 ns | 90.532 ns | 84.684 ns | 14,496.20 ns | 2.3804 | 0.2899 | - | 40,056 B |
ListAsEnumerablePreAllocated | 10000 | 54,068.01 ns | 883.669 ns | 737.903 ns | 54,336.54 ns | 2.3804 | 0.2441 | - | 40,096 B |
List | 10000 | 25,751.59 ns | 439.144 ns | 389.290 ns | 25,673.67 ns | 7.8125 | 1.5564 | - | 131,400 B |
ListFor | 10000 | 21,271.87 ns | 411.223 ns | 562.887 ns | 21,305.89 ns | 7.8125 | 1.5564 | - | 131,400 B |
ListAsEnumerable | 10000 | 59,762.03 ns | 560.015 ns | 523.838 ns | 59,660.14 ns | 7.8125 | 1.5259 | - | 131,440 B |
EnumerableWithStructEnumerator | 10000 | 41,035.79 ns | 509.594 ns | 451.742 ns | 41,156.28 ns | - | - | - | 120 B |
EnumerableWithClassEnumerator | 10000 | 29,664.49 ns | 303.049 ns | 283.472 ns | 29,738.06 ns | - | - | - | 120 B |
StructEnumerator | 10000 | 2,252.60 ns | 41.783 ns | 37.039 ns | 2,257.00 ns | - | - | - | - |
ClassEnumerator | 10000 | 2,854.80 ns | 8.105 ns | 7.581 ns | 2,852.37 ns | - | - | - | 24 B |
Yield | 100000 | 344,209.19 ns | 3,857.413 ns | 3,419.496 ns | 344,522.12 ns | - | - | - | 40 B |
Array | 100000 | 149,007.39 ns | 2,902.816 ns | 2,850.954 ns | 149,197.58 ns | 124.7559 | 124.7559 | 124.7559 | 400,067 B |
SpanArray | 100000 | 165,875.57 ns | 2,831.655 ns | 2,907.901 ns | 165,356.08 ns | 124.7559 | 124.7559 | 124.7559 | 400,067 B |
ArrayAsEnumerable | 100000 | 333,082.45 ns | 5,296.178 ns | 4,954.049 ns | 332,112.74 ns | 124.5117 | 124.5117 | 124.5117 | 400,098 B |
ArrayFor | 100000 | 68,332.48 ns | 1,329.858 ns | 1,681.844 ns | 68,950.29 ns | 124.7559 | 124.7559 | 124.7559 | 400,067 B |
SpanArrayFor | 100000 | 108,060.12 ns | 1,925.023 ns | 1,976.857 ns | 108,488.26 ns | 124.7559 | 124.7559 | 124.7559 | 400,066 B |
ListPreAllocated | 100000 | 266,118.64 ns | 5,173.583 ns | 4,839.373 ns | 267,802.78 ns | 124.5117 | 124.5117 | 124.5117 | 400,098 B |
ListPreAllocatedFor | 100000 | 171,220.91 ns | 2,386.368 ns | 2,115.453 ns | 171,328.39 ns | 124.5117 | 124.5117 | 124.5117 | 400,098 B |
ListAsEnumerablePreAllocated | 100000 | 576,418.53 ns | 7,482.536 ns | 6,633.073 ns | 577,166.46 ns | 124.0234 | 124.0234 | 124.0234 | 400,138 B |
List | 100000 | 377,805.62 ns | 3,571.947 ns | 3,166.438 ns | 377,843.63 ns | 285.6445 | 285.6445 | 285.6445 | 1,049,072 B |
ListFor | 100000 | 254,958.94 ns | 3,977.419 ns | 3,720.480 ns | 255,925.73 ns | 285.6445 | 285.6445 | 285.6445 | 1,049,072 B |
ListAsEnumerable | 100000 | 767,712.23 ns | 4,386.375 ns | 4,103.017 ns | 767,677.15 ns | 285.1563 | 285.1563 | 285.1563 | 1,049,112 B |
EnumerableWithStructEnumerator | 100000 | 410,042.90 ns | 4,283.833 ns | 4,007.100 ns | 410,599.76 ns | - | - | - | 120 B |
EnumerableWithClassEnumerator | 100000 | 267,723.79 ns | 5,174.829 ns | 7,254.388 ns | 270,364.55 ns | - | - | - | 120 B |
StructEnumerator | 100000 | 22,672.77 ns | 124.271 ns | 110.163 ns | 22,666.68 ns | - | - | - | - |
ClassEnumerator | 100000 | 28,518.48 ns | 93.478 ns | 82.866 ns | 28,508.09 ns | - | - | - | 24 B |
Yield | 1000000 | 3,454,202.34 ns | 13,727.279 ns | 12,840.505 ns | 3,451,197.27 ns | - | - | - | 42 B |
Array | 1000000 | 787,463.36 ns | 7,964.110 ns | 7,449.633 ns | 788,737.50 ns | 998.0469 | 998.0469 | 998.0469 | 4,000,365 B |
SpanArray | 1000000 | 797,894.99 ns | 11,867.606 ns | 11,100.966 ns | 797,012.40 ns | 998.0469 | 998.0469 | 998.0469 | 4,000,365 B |
ArrayAsEnumerable | 1000000 | 3,317,991.15 ns | 58,579.152 ns | 54,794.975 ns | 3,341,410.16 ns | 992.1875 | 992.1875 | 992.1875 | 4,000,395 B |
ArrayFor | 1000000 | 791,535.47 ns | 10,392.955 ns | 9,721.576 ns | 790,121.09 ns | 998.0469 | 998.0469 | 998.0469 | 4,000,365 B |
SpanArrayFor | 1000000 | 806,574.45 ns | 15,084.206 ns | 14,109.775 ns | 804,832.62 ns | 998.0469 | 998.0469 | 998.0469 | 4,000,361 B |
ListPreAllocated | 1000000 | 1,949,278.80 ns | 19,825.294 ns | 18,544.592 ns | 1,950,201.56 ns | 996.0938 | 996.0938 | 996.0938 | 4,000,392 B |
ListPreAllocatedFor | 1000000 | 1,483,358.41 ns | 26,740.626 ns | 25,013.198 ns | 1,485,576.17 ns | 996.0938 | 996.0938 | 996.0938 | 4,000,392 B |
ListAsEnumerablePreAllocated | 1000000 | 5,209,356.14 ns | 59,824.790 ns | 53,033.119 ns | 5,214,291.80 ns | 992.1875 | 992.1875 | 992.1875 | 4,000,432 B |
List | 1000000 | 8,770,945.22 ns | 462,934.495 ns | 1,364,972.802 ns | 9,102,647.66 ns | 1835.9375 | 1835.9375 | 1835.9375 | 8,389,636 B |
ListFor | 1000000 | 8,667,393.43 ns | 612,015.132 ns | 1,804,540.424 ns | 9,268,035.55 ns | 1851.5625 | 1851.5625 | 1851.5625 | 8,389,641 B |
ListAsEnumerable | 1000000 | 11,848,900.89 ns | 559,681.728 ns | 1,650,234.203 ns | 12,455,236.72 ns | 1828.1250 | 1828.1250 | 1828.1250 | 8,389,680 B |
EnumerableWithStructEnumerator | 1000000 | 3,912,166.35 ns | 9,122.631 ns | 7,617.810 ns | 3,910,342.97 ns | - | - | - | 122 B |
EnumerableWithClassEnumerator | 1000000 | 2,537,318.11 ns | 37,160.711 ns | 32,942.002 ns | 2,541,284.96 ns | - | - | - | 122 B |
StructEnumerator | 1000000 | 229,377.31 ns | 3,443.707 ns | 3,052.757 ns | 228,607.42 ns | - | - | - | - |
ClassEnumerator | 1000000 | 283,205.60 ns | 4,624.514 ns | 4,325.774 ns | 284,619.78 ns | - | - | - | 24 B |
Yield | 10000000 | 34,803,397.14 ns | 595,110.688 ns | 556,666.906 ns | 34,738,850.00 ns | - | - | - | 67 B |
Array | 10000000 | 11,357,159.56 ns | 207,455.242 ns | 428,431.048 ns | 11,216,584.38 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,192 B |
SpanArray | 10000000 | 11,038,724.06 ns | 143,609.933 ns | 134,332.821 ns | 11,005,789.06 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,192 B |
ArrayAsEnumerable | 10000000 | 36,287,366.07 ns | 335,943.083 ns | 297,804.792 ns | 36,322,279.17 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,244 B |
ArrayFor | 10000000 | 11,190,465.00 ns | 223,106.593 ns | 208,694.046 ns | 11,162,209.38 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,192 B |
SpanArrayFor | 10000000 | 11,001,856.25 ns | 144,205.674 ns | 127,834.574 ns | 11,003,495.31 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,262 B |
ListPreAllocated | 10000000 | 22,746,962.95 ns | 234,397.487 ns | 207,787.267 ns | 22,763,868.75 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,236 B |
ListPreAllocatedFor | 10000000 | 18,061,488.17 ns | 235,328.083 ns | 208,612.216 ns | 18,022,773.44 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,224 B |
ListAsEnumerablePreAllocated | 10000000 | 56,266,840.74 ns | 1,060,286.882 ns | 1,486,374.127 ns | 56,477,850.00 ns | 500.0000 | 500.0000 | 500.0000 | 40,000,300 B |
List | 10000000 | 64,145,683.84 ns | 1,168,883.486 ns | 1,435,493.893 ns | 63,917,733.33 ns | 3000.0000 | 3000.0000 | 3000.0000 | 134,219,253 B |
ListFor | 10000000 | 61,536,427.03 ns | 1,213,656.777 ns | 2,060,881.315 ns | 61,733,311.11 ns | 3222.2222 | 3222.2222 | 3222.2222 | 134,219,323 B |
ListAsEnumerable | 10000000 | 95,748,054.44 ns | 1,855,676.078 ns | 1,735,800.555 ns | 95,772,433.33 ns | 3333.3333 | 3333.3333 | 3333.3333 | 134,221,325 B |
EnumerableWithStructEnumerator | 10000000 | 41,533,448.90 ns | 471,428.118 ns | 417,908.747 ns | 41,400,892.31 ns | - | - | - | 150 B |
EnumerableWithClassEnumerator | 10000000 | 25,452,779.24 ns | 114,554.909 ns | 101,549.943 ns | 25,430,178.12 ns | - | - | - | 132 B |
StructEnumerator | 10000000 | 2,287,743.19 ns | 22,696.487 ns | 21,230.308 ns | 2,277,097.46 ns | - | - | - | 2 B |
ClassEnumerator | 10000000 | 2,850,516.41 ns | 23,453.804 ns | 19,584.988 ns | 2,842,571.48 ns | - | - | - | 26 B |