C# Using Ints Vs Stringified-Ints
This article details the benchmarks and performance showdown in C# using Ints Vs Stringified-Ints.
Often enough C# programmers will find themselves using numbers in their string representation. That is, using “8” as opposed to 8.
In C# though, strings are 1 byte per character whereas ints are 4 bytes.
That means for numbers:
- less than 1000, strings use less memory than ints and thus should be quicker if arithmetic operations aren’t needed
- between 1000 and 9999, their memory footprint should be the same and speeds should be roughly equal
- greater than or equal to 10,000, ints should rule
But! The question is – is this true in practice?
That’s what this Curious Consultant wanted to know.
Do you? Let’s find out!
Getting Started
For these benchmarks, we’ll be doing the following common C# operations:
- if statement comparisons of int == int vs stringified-int == stringified-int
- switch-case statements on ints vs stringified-ints
- adding to Dictionary objects with ints as keys vs stringified-ints as keys
- reading from Dictionary objects with ints as keys vs stringified-ints as keys
The C# is written in Visual Studio 2017 targeting .Net Framework version 4.7.1 x64. The source code is available at the end of this blog so you can benchmark it on your own system.
Who won?
Let’s see what happened on my machine. The tests were performed twice, rebooting the computer before each run to clear the slate.
All times are indicated in seconds.milliseconds format.
Lower numbers indicate faster performance.
|
10 |
1,000 |
100,000 |
10,000,000 |
if “int” comparison |
00.0000019 |
00.0000047 |
00.0005040 |
00.0479261 |
if “stringified- int” comparison |
00.0000011 |
00.0000173 |
00.0018386 |
00.2373046 |
|
||||
switch-case “int” |
00.0000003 |
00.0000158 |
00.0002180 |
00.0162536 |
switch-case “stringified-int” |
00.0000007 |
00.0000169 |
00.0016154 |
00.1505063 |
|
||||
Dictionary add “int” |
00.0000537 |
00.0000114 |
00.0017856 |
00.1707078 |
Dictionary add “stringified-int” |
00.0000154 |
00.0000557 |
00.0039296 |
00.5202994 |
|
||||
Dictionary read “int” |
00.0000371 |
00.0000126 |
00.0014076 |
00.1164386 |
Dictionary read “stringified-int” |
00.0000221 |
00.0000387 |
00.0045665 |
00.5322457 |
|
10 |
1,000 |
100,000 |
10,000,000 |
if “int” comparison |
00.0000059 |
00.0000067 |
00.0006723 |
00.0405404 |
if “stringified- int” comparison |
00.0000011 |
00.0000632 |
00.0074283 |
00.2818466 |
|
||||
switch-case “int” |
00.0000007 |
00.0000059 |
00.0006016 |
00.0345193 |
switch-case “stringified-int” |
00.0000023 |
00.0000628 |
00.0060483 |
00.1571686 |
|
||||
Dictionary add “int” |
00.0007948 |
00.0000450 |
00.0059282 |
00.1772322 |
Dictionary add “stringified-int” |
00.0000402 |
00.0000971 |
00.0124622 |
00.4916288 |
|
||||
Dictionary read “int” |
00.0000643 |
00.0000497 |
00.0044764 |
00.1473004 |
Dictionary read “stringified-int” |
00.0000430 |
00.0001319 |
00.0143146 |
00.5096981 |
The Winning Results
Check out those numbers above.
Unless someone spots a flaw in my code, when performing around 10 comparisons or add/read Dictionary operations, the stringified values win hands down. Except with the switch-case statements which I found puzzling as I thought strings would have won everywhere.
When there’s 1,000 or more comparisons, the performance numbers switched back in favor of ints by clear margins.
What does this mean?
Well, using numbers as ints seems like a no-brainer. But there are systems and situations where they need their string-representation to be used.
If you need speed and have to micro-optimize every nano-second, in the majority of cases, ints should be used as they performed faster by a factor of 10 in most cases.
The Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
using System; using System.Collections.Generic; using System.Collections; using System.Collections.Concurrent; using System.Data; using System.Data.Sql; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; using System.Threading; using System.Diagnostics; namespace TestApplication { class Program { static void Main(string[] args) { DateTime end; DateTime start = DateTime.Now; Console.WriteLine("### Overall Start Time: " + start.ToLongTimeString()); Console.WriteLine(); TestIntsVsStringifiedInts(10); TestIntsVsStringifiedInts(1000); TestIntsVsStringifiedInts(100000); TestIntsVsStringifiedInts(10000000); end = DateTime.Now; Console.WriteLine(); Console.WriteLine("### Overall End Time: " + end.ToLongTimeString()); Console.WriteLine("### Overall Run Time: " + (end - start)); Console.WriteLine(); Console.WriteLine("Hit Enter to Exit"); Console.ReadLine(); } //#################################################### //Compare the speed differences between using ints and "stringified" ints. //We're assuming no exceptions so we don't implement exception //handling which will affect code performance. //Code from http://cc.davelozinski.com static void TestIntsVsStringifiedInts(int numberOfNumbersToTest) { //for initializing arrays and using in switch-case statements const string testNumberOneString = "1"; const int testNumberOneInt = 1; const string testNumberTwoString = "10"; const int testNumberTwoInt = 10; const string testNumberThreeString = "801"; const int testNumberThreeInt = 801; const string testNumberFourString = "3333"; const int testNumberFourInt = 3333; const string testNumberFiveString = "55555"; const int testNumberFiveInt = 55555; const string testNumberSixString = "222222"; const int testNumberSixInt = 222222; Console.WriteLine("######## " + System.Reflection.MethodBase.GetCurrentMethod().Name); Console.WriteLine("Number of values to convert: " + numberOfNumbersToTest.ToString("#,##0")); Stopwatch sw = new Stopwatch(); DateTime end = DateTime.Now; DateTime start = DateTime.Now; long total = 0; //Will hold all our numbers in both "stringified" and int form as the value //We'll use these for testing, such as adding to Dictionary. string[] s = new string[numberOfNumbersToTest]; int[] i = new int[numberOfNumbersToTest]; //Will hold all the numbers we will test against. //Just chose random numbers. string[] st = new string[] { testNumberOneString, testNumberTwoString, testNumberThreeString, testNumberFourString, testNumberFiveString, testNumberSixString }; int[] it = new int[] { testNumberOneInt, testNumberTwoInt, testNumberThreeInt, testNumberFourInt, testNumberFiveInt, testNumberSixInt }; //Test adding/reading from a dictionary with keys as ints and keys as stringified-ints. Dictionary<string, int> ds = new Dictionary<string, int>(numberOfNumbersToTest); Dictionary<int, int> di = new Dictionary<int, int>(numberOfNumbersToTest); //Initialize the arrays for (int x = 0; x < numberOfNumbersToTest; x++) { i[x] = x; s[x] = x.ToString(); } //if int comparison test Console.WriteLine("###########################################################"); Console.WriteLine("Starting if-int comparison test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { for (int y=0; y < it.Length; y++) if (i[x] == it[y]) total += x; } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //if stringified-int comparison test Console.WriteLine("###########################################################"); Console.WriteLine("Starting if stringified-int comparison test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { for (int y = 0; y < st.Length; y++) if (s[x] == st[y]) total += x; } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //switch-case int comparison test //Since switch-case is fixed, we have to hard-code in the entries -- can't make it dynamic. Console.WriteLine("###########################################################"); Console.WriteLine("Starting switch-case int comparison test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { switch (i[x]) { case testNumberOneInt: case testNumberTwoInt: case testNumberThreeInt: case testNumberFourInt: case testNumberFiveInt: case testNumberSixInt: total += x; break; default: break; } } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //switch-case stringified-int comparison test //Since switch-case is fixed, we have to hard-code in the entries -- can't make it dynamic. Console.WriteLine("###########################################################"); Console.WriteLine("Starting switch-case stringified-int comparison test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { switch (s[x]) { case testNumberOneString: case testNumberTwoString: case testNumberThreeString: case testNumberFourString: case testNumberFiveString: case testNumberSixString: total += x; break; default: break; } } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //Adding int key to dictionary Console.WriteLine("###########################################################"); Console.WriteLine("Starting adding int-key to dictionary test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { di.Add(i[x], i[x]); } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + di.Count.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Thread.Sleep(500); //Adding stringified-int key to dictionary Console.WriteLine("###########################################################"); Console.WriteLine("Starting adding stringified-int-key to dictionary test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { ds.Add(s[x], i[x]); } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + ds.Count.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); Thread.Sleep(500); //Reading int key from dictionary Console.WriteLine("###########################################################"); Console.WriteLine("Starting reading int-key from dictionary test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { total += di[i[x]]; } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //Reading stringified int key from dictionary Console.WriteLine("###########################################################"); Console.WriteLine("Starting reading stringified-int-key from dictionary test " + numberOfNumbersToTest.ToString("#,##0") + " at: " + DateTime.Now.ToLongTimeString()); sw.Restart(); for (int x = 0; x < numberOfNumbersToTest; x++) { total += ds[s[x]]; } sw.Stop(); Console.WriteLine("Finished at: " + DateTime.Now.ToLongTimeString()); Console.WriteLine("Total: " + total.ToString("#,##0")); Console.WriteLine("Time to run: " + sw.Elapsed.ToString("mm\\:ss\\.fffffff")); total = 0; Thread.Sleep(500); //cleanup Array.Clear(i, 0, i.Length); Array.Clear(s, 0, s.Length); i = null; s = null; Array.Clear(it, 0, it.Length); Array.Clear(st, 0, st.Length); it = null; st = null; di.Clear(); ds.Clear(); di = null; ds = null; Console.WriteLine(); } } //class } //namespace |