├── src
├── app.config
├── packages.config
├── Dictionary.csproj
├── Performance.cs
├── Tests.cs
└── FastDictionary.cs
├── LICENSE
├── Dictionary.sln
└── .gitignore
/src/app.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/src/packages.config:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2015 Federico Andres Lois
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
23 |
--------------------------------------------------------------------------------
/Dictionary.sln:
--------------------------------------------------------------------------------
1 |
2 | Microsoft Visual Studio Solution File, Format Version 12.00
3 | # Visual Studio 2013
4 | VisualStudioVersion = 12.0.31101.0
5 | MinimumVisualStudioVersion = 10.0.40219.1
6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Dictionary", "src\Dictionary.csproj", "{77367691-7DF8-408A-A8BE-3D0F787DE388}"
7 | EndProject
8 | Global
9 | GlobalSection(SolutionConfigurationPlatforms) = preSolution
10 | Debug|Any CPU = Debug|Any CPU
11 | Release|Any CPU = Release|Any CPU
12 | EndGlobalSection
13 | GlobalSection(ProjectConfigurationPlatforms) = postSolution
14 | {77367691-7DF8-408A-A8BE-3D0F787DE388}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15 | {77367691-7DF8-408A-A8BE-3D0F787DE388}.Debug|Any CPU.Build.0 = Debug|Any CPU
16 | {77367691-7DF8-408A-A8BE-3D0F787DE388}.Release|Any CPU.ActiveCfg = Release|Any CPU
17 | {77367691-7DF8-408A-A8BE-3D0F787DE388}.Release|Any CPU.Build.0 = Release|Any CPU
18 | EndGlobalSection
19 | GlobalSection(SolutionProperties) = preSolution
20 | HideSolutionNode = FALSE
21 | EndGlobalSection
22 | GlobalSection(CodealikeProperties) = postSolution
23 | SolutionGuid = 8b733967-fb73-4e55-8512-76aeff438d47
24 | EndGlobalSection
25 | EndGlobal
26 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Ignore Visual Studio temporary files, build results, and
2 | ## files generated by popular Visual Studio add-ons.
3 |
4 | # User-specific files
5 | *.suo
6 | *.user
7 | *.userosscache
8 | *.sln.docstates
9 |
10 | # User-specific files (MonoDevelop/Xamarin Studio)
11 | *.userprefs
12 |
13 | # Build results
14 | [Dd]ebug/
15 | [Dd]ebugPublic/
16 | [Rr]elease/
17 | [Rr]eleases/
18 | x64/
19 | x86/
20 | build/
21 | bld/
22 | [Bb]in/
23 | [Oo]bj/
24 |
25 | # Visual Studo 2015 cache/options directory
26 | .vs/
27 |
28 | # MSTest test Results
29 | [Tt]est[Rr]esult*/
30 | [Bb]uild[Ll]og.*
31 |
32 | # NUNIT
33 | *.VisualState.xml
34 | TestResult.xml
35 |
36 | # Build Results of an ATL Project
37 | [Dd]ebugPS/
38 | [Rr]eleasePS/
39 | dlldata.c
40 |
41 | *_i.c
42 | *_p.c
43 | *_i.h
44 | *.ilk
45 | *.meta
46 | *.obj
47 | *.pch
48 | *.pdb
49 | *.pgc
50 | *.pgd
51 | *.rsp
52 | *.sbr
53 | *.tlb
54 | *.tli
55 | *.tlh
56 | *.tmp
57 | *.tmp_proj
58 | *.log
59 | *.vspscc
60 | *.vssscc
61 | .builds
62 | *.pidb
63 | *.svclog
64 | *.scc
65 |
66 | # Chutzpah Test files
67 | _Chutzpah*
68 |
69 | # Visual C++ cache files
70 | ipch/
71 | *.aps
72 | *.ncb
73 | *.opensdf
74 | *.sdf
75 | *.cachefile
76 |
77 | # Visual Studio profiler
78 | *.psess
79 | *.vsp
80 | *.vspx
81 |
82 | # TFS 2012 Local Workspace
83 | $tf/
84 |
85 | # Guidance Automation Toolkit
86 | *.gpState
87 |
88 | # ReSharper is a .NET coding add-in
89 | _ReSharper*/
90 | *.[Rr]e[Ss]harper
91 | *.DotSettings.user
92 |
93 | # JustCode is a .NET coding addin-in
94 | .JustCode
95 |
96 | # TeamCity is a build add-in
97 | _TeamCity*
98 |
99 | # DotCover is a Code Coverage Tool
100 | *.dotCover
101 |
102 | # NCrunch
103 | _NCrunch_*
104 | .*crunch*.local.xml
105 |
106 | # MightyMoose
107 | *.mm.*
108 | AutoTest.Net/
109 |
110 | # Web workbench (sass)
111 | .sass-cache/
112 |
113 | # Installshield output folder
114 | [Ee]xpress/
115 |
116 | # DocProject is a documentation generator add-in
117 | DocProject/buildhelp/
118 | DocProject/Help/*.HxT
119 | DocProject/Help/*.HxC
120 | DocProject/Help/*.hhc
121 | DocProject/Help/*.hhk
122 | DocProject/Help/*.hhp
123 | DocProject/Help/Html2
124 | DocProject/Help/html
125 |
126 | # Click-Once directory
127 | publish/
128 |
129 | # Publish Web Output
130 | *.[Pp]ublish.xml
131 | *.azurePubxml
132 | # TODO: Comment the next line if you want to checkin your web deploy settings
133 | # but database connection strings (with potential passwords) will be unencrypted
134 | *.pubxml
135 | *.publishproj
136 |
137 | # NuGet Packages
138 | *.nupkg
139 | # The packages folder can be ignored because of Package Restore
140 | **/packages/*
141 | # except build/, which is used as an MSBuild target.
142 | !**/packages/build/
143 | # Uncomment if necessary however generally it will be regenerated when needed
144 | #!**/packages/repositories.config
145 |
146 | # Windows Azure Build Output
147 | csx/
148 | *.build.csdef
149 |
150 | # Windows Store app package directory
151 | AppPackages/
152 |
153 | # Others
154 | *.[Cc]ache
155 | ClientBin/
156 | [Ss]tyle[Cc]op.*
157 | ~$*
158 | *~
159 | *.dbmdl
160 | *.dbproj.schemaview
161 | *.pfx
162 | *.publishsettings
163 | node_modules/
164 | bower_components/
165 |
166 | # RIA/Silverlight projects
167 | Generated_Code/
168 |
169 | # Backup & report files from converting an old project file
170 | # to a newer Visual Studio version. Backup files are not needed,
171 | # because we have git ;-)
172 | _UpgradeReport_Files/
173 | Backup*/
174 | UpgradeLog*.XML
175 | UpgradeLog*.htm
176 |
177 | # SQL Server files
178 | *.mdf
179 | *.ldf
180 |
181 | # Business Intelligence projects
182 | *.rdl.data
183 | *.bim.layout
184 | *.bim_*.settings
185 |
186 | # Microsoft Fakes
187 | FakesAssemblies/
188 |
189 | # Node.js Tools for Visual Studio
190 | .ntvs_analysis.dat
191 |
192 | # Visual Studio 6 build log
193 | *.plg
194 |
195 | # Visual Studio 6 workspace options file
196 | *.opt
197 |
--------------------------------------------------------------------------------
/src/Dictionary.csproj:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | Debug
8 | AnyCPU
9 | {77367691-7DF8-408A-A8BE-3D0F787DE388}
10 | Exe
11 | Properties
12 | Dictionary
13 | Dictionary
14 | v4.5
15 | 512
16 | 53c07c1c
17 | 0
18 | publish\
19 | true
20 | Disk
21 | false
22 | Foreground
23 | 7
24 | Days
25 | false
26 | false
27 | true
28 | 0
29 | 1.0.0.%2a
30 | false
31 | false
32 | true
33 |
34 |
35 |
36 | AnyCPU
37 | true
38 | full
39 | false
40 | bin\Debug\
41 | DEBUG;TRACE
42 | prompt
43 | 4
44 | true
45 | false
46 | True
47 | False
48 | True
49 | False
50 | False
51 | True
52 | True
53 | True
54 | True
55 | True
56 | True
57 | True
58 | True
59 | True
60 | False
61 | True
62 | True
63 | True
64 | True
65 | False
66 | False
67 | False
68 | True
69 | False
70 | True
71 | True
72 | True
73 | False
74 | False
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 | True
83 | False
84 | False
85 | False
86 | Full
87 | %28none%29
88 | 0
89 |
90 |
91 | AnyCPU
92 | pdbonly
93 | true
94 | bin\Release\
95 | TRACE
96 | prompt
97 | 4
98 | true
99 | false
100 |
101 |
102 | Dictionary.Performance
103 |
104 |
105 |
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 | packages\xunit.abstractions.2.0.0\lib\net35\xunit.abstractions.dll
114 | True
115 |
116 |
117 | packages\xunit.assert.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.assert.dll
118 | True
119 |
120 |
121 | packages\xunit.extensibility.core.2.0.0\lib\portable-net45+win+wpa81+wp80+monotouch+monoandroid+Xamarin.iOS\xunit.core.dll
122 | True
123 |
124 |
125 |
126 |
127 |
128 |
129 |
130 |
131 |
132 |
133 |
134 |
135 |
136 |
137 |
138 |
139 | False
140 | Microsoft .NET Framework 4.5 %28x86 and x64%29
141 | true
142 |
143 |
144 | False
145 | .NET Framework 3.5 SP1 Client Profile
146 | false
147 |
148 |
149 | False
150 | .NET Framework 3.5 SP1
151 | false
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 | This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}.
161 |
162 |
163 |
164 |
165 |
172 |
--------------------------------------------------------------------------------
/src/Performance.cs:
--------------------------------------------------------------------------------
1 | using System;
2 | using System.Collections.Generic;
3 | using System.Diagnostics;
4 | using System.Linq;
5 | using System.Runtime.CompilerServices;
6 | using System.Text;
7 | using System.Threading.Tasks;
8 |
9 | namespace Dictionary
10 | {
11 | public class Performance
12 | {
13 | public static void Main ()
14 | {
15 | Process.GetCurrentProcess().PriorityClass = ProcessPriorityClass.RealTime;
16 |
17 | Random rnd = new Random(13);
18 | int[] tuples = new int[1000000];
19 | string[] tuplesString = new string[1000000];
20 | for (int i = 0; i < tuples.Length; i++)
21 | {
22 | tuples[i] = rnd.Next();
23 | tuplesString[i] = tuples[i].ToString();
24 | }
25 |
26 | Console.WriteLine("Structs: " + BenchmarkCreationOfArrayOfStructs());
27 | Console.WriteLine("Arrays: " + BenchmarkCreationOfMultipleArrays());
28 |
29 | int tries = 100000;
30 | Console.WriteLine("Structs with temps: " + BenchmarkAccessWithTemps(tries));
31 | Console.WriteLine("Structs without temps: " + BenchmarkAccessWithoutTemps(tries));
32 | Console.WriteLine("Structs with inline references: " + BenchmarkAccessWithInlineRef(tries));
33 |
34 | tries = 5;
35 |
36 | Console.WriteLine("Native: " + BenchmarkNativeDictionary(tuples, tries));
37 | Console.WriteLine("Fast: " + BenchmarkFastDictionary(tuples, tries));
38 |
39 | Console.WriteLine("Native-String: " + BenchmarkNativeDictionaryString(tuplesString, tries));
40 | Console.WriteLine("Fast-String: " + BenchmarkFastDictionaryString(tuplesString, tries));
41 |
42 | Console.WriteLine("Native-String-Out: " + BenchmarkNativeDictionaryStringOut(tuplesString, tries));
43 | Console.WriteLine("Fast-String-Out: " + BenchmarkFastDictionaryStringOut(tuplesString, tries));
44 | }
45 |
46 | private static long BenchmarkNativeDictionary(int[] tuples, int tries)
47 | {
48 | var native = Stopwatch.StartNew();
49 | for (int i = 0; i < tries; i++)
50 | {
51 | Dictionary nativeDict = new Dictionary(tuples.Length * 2);
52 | for (int j = 0; j < tuples.Length; j++)
53 | nativeDict[tuples[j]] = j;
54 |
55 | int k;
56 | for (int j = 0; j < tuples.Length; j++)
57 | {
58 | k = nativeDict[tuples[j]];
59 | k++;
60 | }
61 |
62 | }
63 | native.Stop();
64 | return native.ElapsedMilliseconds;
65 | }
66 |
67 | private static long BenchmarkNativeDictionaryString(string[] tuples, int tries)
68 | {
69 | var native = Stopwatch.StartNew();
70 | for (int i = 0; i < tries; i++)
71 | {
72 | var nativeDict = new Dictionary(tuples.Length * 2);
73 | for (int j = 0; j < tuples.Length; j++)
74 | nativeDict[tuples[j]] = j;
75 |
76 | int k;
77 | for (int j = 0; j < tuples.Length; j++)
78 | {
79 | k = nativeDict[tuples[j]];
80 | k++;
81 | }
82 |
83 | }
84 | native.Stop();
85 | return native.ElapsedMilliseconds;
86 | }
87 |
88 | private static long BenchmarkNativeDictionaryStringOut(string[] tuples, int tries)
89 | {
90 | int y = 0;
91 | var native = Stopwatch.StartNew();
92 | for (int i = 0; i < tries; i++)
93 | {
94 | var nativeDict = new Dictionary(tuples.Length * 2);
95 | for (int j = 0; j < tuples.Length; j++)
96 | nativeDict[j] = tuples[j];
97 |
98 | string k;
99 | for (int j = 0; j < tuples.Length; j++)
100 | {
101 | k = nativeDict[j];
102 | if (k != null)
103 | y++;
104 | }
105 |
106 |
107 | }
108 | native.Stop();
109 | return native.ElapsedMilliseconds;
110 | }
111 |
112 | private static long BenchmarkFastDictionary(int[] tuples, int tries)
113 | {
114 | var fast = Stopwatch.StartNew();
115 | for (int i = 0; i < tries; i++)
116 | {
117 | var fastDict = new FastDictionary(tuples.Length * 2);
118 | for (int j = 0; j < tuples.Length; j++)
119 | fastDict[tuples[j]] = j;
120 |
121 | int k;
122 | for (int j = 0; j < tuples.Length; j++)
123 | {
124 | fastDict.TryGetValue(tuples[j], out k);
125 | // k = fastDict[tuples[j]];
126 | k++;
127 | }
128 |
129 |
130 | }
131 | fast.Stop();
132 | return fast.ElapsedMilliseconds;
133 |
134 | }
135 |
136 | private static long BenchmarkFastDictionaryString(string[] tuples, int tries)
137 | {
138 | var fast = Stopwatch.StartNew();
139 | for (int i = 0; i < tries; i++)
140 | {
141 | var fastDict = new FastDictionary(tuples.Length * 2);
142 | for (int j = 0; j < tuples.Length; j++)
143 | fastDict[tuples[j]] = j;
144 |
145 | int k;
146 | for (int j = 0; j < tuples.Length; j++)
147 | {
148 | fastDict.TryGetValue(tuples[j], out k);
149 | // k = fastDict[tuples[j]];
150 | k++;
151 | }
152 |
153 | }
154 | fast.Stop();
155 | return fast.ElapsedMilliseconds;
156 | }
157 |
158 |
159 | private static long BenchmarkFastDictionaryStringOut(string[] tuples, int tries)
160 | {
161 | int y = 0;
162 | var fast = Stopwatch.StartNew();
163 | for (int i = 0; i < tries; i++)
164 | {
165 | var fastDict = new FastDictionary(tuples.Length * 2);
166 | for (int j = 0; j < tuples.Length; j++)
167 | fastDict[j] = tuples[j];
168 |
169 | string k;
170 | for (int j = 0; j < tuples.Length; j++)
171 | {
172 | fastDict.TryGetValue(j, out k);
173 | //k = fastDict[j];
174 | if (k != null)
175 | y++;
176 | }
177 |
178 | }
179 | fast.Stop();
180 | return fast.ElapsedMilliseconds;
181 | }
182 |
183 |
184 |
185 |
186 | public struct TryEntry
187 | {
188 | public uint Hash;
189 | public TKey Key;
190 | public TValue Value;
191 | }
192 |
193 | private const int iterations = 4000;
194 |
195 | public static long BenchmarkCreationOfArrayOfStructs()
196 | {
197 | var fast = Stopwatch.StartNew();
198 |
199 | TryEntry