1+ using BenchmarkDotNet . Attributes ;
2+ using BenchmarkDotNet . Jobs ;
3+
4+ namespace Benchmark . Queue
5+ {
6+ [ MemoryDiagnoser ]
7+ [ SimpleJob ( RuntimeMoniker . Net60 ) ]
8+ [ HideColumns ( "Error" , "StdDev" , "Median" , "Gen0" , "Gen1" , "Gen2" , "Alloc Ratio" , "RatioSD" ) ]
9+ public class PrimitiveOptimalJob
10+ {
11+ [ Params ( 100 , 1000 , 10000 , 100000 , 250000 , 500000 , 1000000 ) ]
12+ public int Size ;
13+
14+ [ Benchmark ( Description = $ "StackMemoryCollections") ]
15+ public void StackMemory ( )
16+ {
17+ unsafe
18+ {
19+ using ( var memory = new StackMemoryCollections . Struct . StackMemory ( sizeof ( int ) * ( nuint ) Size ) )
20+ {
21+ for ( int j = 0 ; j < 100 ; j ++ )
22+ {
23+ {
24+ using var stack = new StackMemoryCollections . Struct . QueueOfInt32 ( ( nuint ) Size , & memory ) ;
25+ for ( int i = 0 ; i < Size ; i ++ )
26+ {
27+ * stack . BackFuture ( ) = i;
28+ stack . PushFuture ( ) ;
29+ }
30+
31+ if ( j > 50 )
32+ {
33+ stack . Clear ( ) ;
34+ }
35+ else
36+ {
37+ while ( stack . TryPop ( ) )
38+ {
39+ }
40+ }
41+ }
42+
43+ using var stack2 = new StackMemoryCollections . Struct . QueueOfInt32 ( ( nuint ) Size , & memory ) ;
44+ for ( int i = 0 ; i < Size ; i ++ )
45+ {
46+ * stack2 . BackFuture ( ) = i;
47+ stack2 . PushFuture ( ) ;
48+ }
49+
50+ if ( j > 50 )
51+ {
52+ stack2 . Clear ( ) ;
53+ }
54+ else
55+ {
56+ while ( stack2 . TryPop ( ) )
57+ {
58+ }
59+ }
60+ }
61+ }
62+ }
63+ }
64+
65+ [ Benchmark ( Baseline = true , Description = "System.Collections.Generic" ) ]
66+ public void SystemCollectionsStack ( )
67+ {
68+ unsafe
69+ {
70+ for ( int j = 0 ; j < 100 ; j ++ )
71+ {
72+ {
73+ var stack = new System . Collections . Generic . Queue < int > ( Size ) ;
74+ for ( int i = 0 ; i < Size ; i ++ )
75+ {
76+ stack . Enqueue ( i ) ;
77+ }
78+
79+ if ( j > 50 )
80+ {
81+ stack . Clear ( ) ;
82+ }
83+ else
84+ {
85+ while ( stack . TryDequeue ( out _ ) )
86+ {
87+ }
88+ }
89+ }
90+
91+ var stack2 = new System . Collections . Generic . Queue < int > ( Size ) ;
92+ for ( int i = 0 ; i < Size ; i ++ )
93+ {
94+ stack2 . Enqueue ( i ) ;
95+ }
96+
97+ if ( j > 50 )
98+ {
99+ stack2 . Clear ( ) ;
100+ }
101+ else
102+ {
103+ while ( stack2 . TryDequeue ( out _ ) )
104+ {
105+ }
106+ }
107+ }
108+ }
109+ }
110+ }
111+ }
0 commit comments