-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
v12 uses the stringbuilder passed into the raw stream handling. This drops allocations from 32kb to 16kb.
- Loading branch information
1 parent
0c04c6b
commit e8f4bee
Showing
16 changed files
with
342 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,4 +256,5 @@ paket-files/ | |
|
||
# JetBrains Rider | ||
.idea/ | ||
*.sln.iml | ||
*.sln.iml | ||
StringsAreEvil/example-input.csv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,12 @@ | ||
namespace StringsAreEvil | ||
using System.Text; | ||
|
||
namespace StringsAreEvil | ||
{ | ||
public interface ILineParser | ||
{ | ||
void ParseLine(string line); | ||
void ParseLine(char[] line); | ||
void Dump(); | ||
void ParseLine(StringBuilder line); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
|
||
namespace StringsAreEvil | ||
{ | ||
/// <summary> | ||
/// Stats:- | ||
/// Took: 6,328 ms | ||
/// Allocated: 16 kb | ||
/// Peak Working Set: 10,780 kb | ||
/// | ||
/// Change:- | ||
/// Passed into the stringbuilder | ||
/// </summary> | ||
public sealed class LineParserV12 : ILineParser | ||
{ | ||
private List<ValueHolderAsStruct> list = new List<ValueHolderAsStruct>(); | ||
|
||
public void ParseLine(string line) | ||
{ | ||
} | ||
|
||
public void ParseLine(char[] line) | ||
{ | ||
|
||
} | ||
|
||
public void Dump() | ||
{ | ||
File.WriteAllLines(@"..\..\v11.txt", list.Select(x => x.ToString())); | ||
} | ||
|
||
public void ParseLine(StringBuilder line) | ||
{ | ||
if (line[0] == 'M' && line[1] == 'N' && line[2] == 'O') | ||
{ | ||
int elementId = ParseSectionAsInt(line, 1); // equal to parts[1] - element id | ||
int vehicleId = ParseSectionAsInt(line, 2); // equal to parts[2] - vehicle id | ||
int term = ParseSectionAsInt(line, 3); // equal to parts[3] - term | ||
int mileage = ParseSectionAsInt(line, 4); // equal to parts[4] - mileage | ||
decimal value = ParseSectionAsDecimal(line, 5); // equal to parts[5] - value | ||
var valueHolder = new ValueHolderAsStruct(elementId, vehicleId, term, mileage, value); | ||
//list.Add(valueHolder); | ||
|
||
} | ||
} | ||
|
||
private static decimal ParseSectionAsDecimal(StringBuilder line, int numberOfCommasToSkip) | ||
{ | ||
decimal val = 0; | ||
bool seenDot = false; | ||
ulong fractionCounter = 10; | ||
int counter = 0; | ||
bool flip = false; | ||
|
||
for (var index = 0; index < line.Length; index++) | ||
{ | ||
// move along the line until we have skipped the required amount of commas | ||
if (line[index] == ',') | ||
{ | ||
counter++; | ||
|
||
if (counter > numberOfCommasToSkip) | ||
{ | ||
break; | ||
} | ||
continue; | ||
} | ||
|
||
// we have skipped enough commas, the next section before the upcoming comma is what we are interested in | ||
if (counter == numberOfCommasToSkip) | ||
{ | ||
// the number is a negative means we have to flip it at the end. | ||
if (line[index] == '-') | ||
{ | ||
flip = true; | ||
continue; | ||
} | ||
|
||
if (line[index] == '.') | ||
{ | ||
seenDot = true; | ||
continue; | ||
} | ||
|
||
// before the . eg; 12.34 this looks for the 12 | ||
if (char.IsNumber(line[index]) && seenDot == false) | ||
{ | ||
val *= 10; | ||
val += line[index] - '0'; | ||
continue; | ||
} | ||
|
||
// after the . eg; 12.34 this looks for the 34 | ||
if (char.IsNumber(line[index]) && seenDot == true) | ||
{ | ||
val += decimal.Divide(line[index] - '0', fractionCounter); | ||
fractionCounter *= 10; | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
return flip ? -val : val; | ||
} | ||
|
||
private static int ParseSectionAsInt(StringBuilder line, int numberOfCommasToSkip) | ||
{ | ||
int val = 0; | ||
int counter = 0; | ||
bool flip = false; | ||
|
||
for (var index = 0; index < line.Length; index++) | ||
{ | ||
// move along the line until we have skipped the required amount of commas | ||
if (line[index] == ',') | ||
{ | ||
counter++; | ||
|
||
if (counter > numberOfCommasToSkip) | ||
{ | ||
break; | ||
} | ||
|
||
continue; | ||
} | ||
|
||
// we have skipped enough commas, the next section before the upcoming comma is what we are interested in | ||
if (counter == numberOfCommasToSkip) | ||
{ | ||
// the number is a negative means we have to flip it at the end. | ||
if (line[index] == '-') | ||
{ | ||
flip = true; | ||
continue; | ||
} | ||
|
||
val *= 10; | ||
val += line[index] - '0'; | ||
} | ||
} | ||
|
||
return flip ? -val : val; | ||
} | ||
} | ||
} |
Oops, something went wrong.