Skip to content

Instantly share code, notes, and snippets.

@rpgmaker
Created September 6, 2014 19:59
Show Gist options
  • Save rpgmaker/c04ceee9a2cdddc0458a to your computer and use it in GitHub Desktop.
Save rpgmaker/c04ceee9a2cdddc0458a to your computer and use it in GitHub Desktop.
Test Performance of NetJSON Long to String in comparision to ToString(CultureInfo)
static void Test(string name, Action action) {
var count = 10000;
var stopWatch = new Stopwatch();
stopWatch.Start();
for (var i = 0; i < count; i++)
action();
stopWatch.Stop();
Console.WriteLine("Iteration: {0}", count);
Console.WriteLine("Completed {0} in Avg {1} Milliseconds", name, stopWatch.ElapsedMilliseconds);
Console.WriteLine();
}
//This logic is the LongToStr method in NetJSON
unsafe static string itoa_int64(long snum) {
char* s = stackalloc char[21];
char* ps = s;
long num1 = snum, num2, num3, num4, num5, div;
if (snum < 0) {
*ps++ = '-';
if (snum == -9223372036854775808) return "-9223372036854775808";
num1 = -snum;
}
if (num1 < 10000) {
if (num1 < 10) goto L1;
if (num1 < 100) goto L2;
if (num1 < 1000) goto L3;
} else {
num2 = num1 / 10000;
num1 -= num2 * 10000;
if (num2 < 10000) {
if (num2 < 10) goto L5;
if (num2 < 100) goto L6;
if (num2 < 1000) goto L7;
} else {
num3 = num2 / 10000;
num2 -= num3 * 10000;
if (num3 < 10000) {
if (num3 < 10) goto L9;
if (num3 < 100) goto L10;
if (num3 < 1000) goto L11;
} else {
num4 = num3 / 10000;
num3 -= num4 * 10000;
if (num4 < 10000) {
if (num4 < 10) goto L13;
if (num4 < 100) goto L14;
if (num4 < 1000) goto L15;
} else {
num5 = num4 / 10000;
num4 -= num5 * 10000;
if (num5 < 10000) {
if (num5 < 10) goto L17;
if (num5 < 100) goto L18;
}
*ps++ = (char)('0' + (div = (num5 * 5243) >> 19));
num5 -= div * 100;
L18:
*ps++ = (char)('0' + (div = (num5 * 6554) >> 16));
num5 -= div * 10;
L17:
*ps++ = (char)('0' + (num5));
}
*ps++ = (char)('0' + (div = (num4 * 8389) >> 23));
num4 -= div * 1000;
L15:
*ps++ = (char)('0' + (div = (num4 * 5243) >> 19));
num4 -= div * 100;
L14:
*ps++ = (char)('0' + (div = (num4 * 6554) >> 16));
num4 -= div * 10;
L13:
*ps++ = (char)('0' + (num4));
}
*ps++ = (char)('0' + (div = (num3 * 8389) >> 23));
num3 -= div * 1000;
L11:
*ps++ = (char)('0' + (div = (num3 * 5243) >> 19));
num3 -= div * 100;
L10:
*ps++ = (char)('0' + (div = (num3 * 6554) >> 16));
num3 -= div * 10;
L9:
*ps++ = (char)('0' + (num3));
}
*ps++ = (char)('0' + (div = (num2 * 8389) >> 23));
num2 -= div * 1000;
L7:
*ps++ = (char)('0' + (div = (num2 * 5243) >> 19));
num2 -= div * 100;
L6:
*ps++ = (char)('0' + (div = (num2 * 6554) >> 16));
num2 -= div * 10;
L5:
*ps++ = (char)('0' + (num2));
}
*ps++ = (char)('0' + (div = (num1 * 8389) >> 23));
num1 -= div * 1000;
L3:
*ps++ = (char)('0' + (div = (num1 * 5243) >> 19));
num1 -= div * 100;
L2:
*ps++ = (char)('0' + (div = (num1 * 6554) >> 16));
num1 -= div * 10;
L1:
*ps++ = (char)('0' + (num1));
return new string(s);
}
static char[] digitr =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToArray();
static char[] rev_3digit_lut =
String.Join(string.Empty, new string[] {"000100200300400500600700800900010110210310410510610710810910020120220320420",
"520620720820920030130230330430530630730830930040140240340440540640740840940",
"050150250350450550650750850950060160260360460560660760860960070170270370470",
"570670770870970080180280380480580680780880980090190290390490590690790890990",
"001101201301401501601701801901011111211311411511611711811911021121221321421",
"521621721821921031131231331431531631731831931041141241341441541641741841941",
"051151251351451551651751851951061161261361461561661761861961071171271371471",
"571671771871971081181281381481581681781881981091191291391491591691791891991",
"002102202302402502602702802902012112212312412512612712812912022122222322422",
"522622722822922032132232332432532632732832932042142242342442542642742842942",
"052152252352452552652752852952062162262362462562662762862962072172272372472",
"572672772872972082182282382482582682782882982092192292392492592692792892992",
"003103203303403503603703803903013113213313413513613713813913023123223323423",
"523623723823923033133233333433533633733833933043143243343443543643743843943",
"053153253353453553653753853953063163263363463563663763863963073173273373473",
"573673773873973083183283383483583683783883983093193293393493593693793893993",
"004104204304404504604704804904014114214314414514614714814914024124224324424",
"524624724824924034134234334434534634734834934044144244344444544644744844944",
"054154254354454554654754854954064164264364464564664764864964074174274374474",
"574674774874974084184284384484584684784884984094194294394494594694794894994",
"005105205305405505605705805905015115215315415515615715815915025125225325425",
"525625725825925035135235335435535635735835935045145245345445545645745845945",
"055155255355455555655755855955065165265365465565665765865965075175275375475",
"575675775875975085185285385485585685785885985095195295395495595695795895995",
"006106206306406506606706806906016116216316416516616716816916026126226326426",
"526626726826926036136236336436536636736836936046146246346446546646746846946",
"056156256356456556656756856956066166266366466566666766866966076176276376476",
"576676776876976086186286386486586686786886986096196296396496596696796896996",
"007107207307407507607707807907017117217317417517617717817917027127227327427",
"527627727827927037137237337437537637737837937047147247347447547647747847947",
"057157257357457557657757857957067167267367467567667767867967077177277377477",
"577677777877977087187287387487587687787887987097197297397497597697797897997",
"008108208308408508608708808908018118218318418518618718818918028128228328428",
"528628728828928038138238338438538638738838938048148248348448548648748848948",
"058158258358458558658758858958068168268368468568668768868968078178278378478",
"578678778878978088188288388488588688788888988098198298398498598698798898998",
"009109209309409509609709809909019119219319419519619719819919029129229329429",
"529629729829929039139239339439539639739839939049149249349449549649749849949",
"059159259359459559659759859959069169269369469569669769869969079179279379479",
"579679779879979089189289389489589689789889989099199299399499599699799899999"}).ToArray();
static char[] rev_2digit_lut =
String.Join(string.Empty, new string[] {
"0010203040506070809001112131415161718191",
"0212223242526272829203132333435363738393",
"0414243444546474849405152535455565758595",
"0616263646566676869607172737475767778797",
"0818283848586878889809192939495969798999"
}).ToArray();
static unsafe string type_to_string_converter_impl_old(long value) {
char* buffer = stackalloc char[24];
char* itr = buffer + 19;
long remainder = 0;
bool negative = value < 0;
if (negative)
value = -1 * value;
if (0 != value) {
long index = 0;
long temp_v = 0;
while (value >= radix_sqr) {
temp_v = value / radix_cube;
remainder = value - (temp_v * radix_cube);
value = temp_v;
index = remainder * 3;
*(itr) = rev_3digit_lut[index];
*(itr - 1) = rev_3digit_lut[index + 1];
*(itr - 2) = rev_3digit_lut[index + 2];
itr -= 3;
}
while (value >= radix) {
temp_v = value / radix_sqr;
remainder = value - (temp_v * radix_sqr);
value = temp_v;
index = remainder << 1;
*(itr--) = rev_2digit_lut[index + 0];
*(itr--) = rev_2digit_lut[index + 1];
}
if (0 != value) {
*(itr--) = digitr[value];
}
} else
*(itr--) = '0';
if (negative) *(itr--) = '-';
itr++;
return new string(itr, 0, (int)((buffer + 24) - itr));
}
static void Main(string[] args){
var l = 5181418955L;
Test("IntUtilityToInt64_1", () => {
for (var i = 0; i < 1000; i++) {
var sx = IntUtility.ltoa(l);
//var xv = "";
}
});
Console.WriteLine(IntUtility.ltoa(l));
Test("IntUtilityToInt64_2", () => {
for (var i = 0; i < 1000; i++) {
var sx = IntUtility.ltoa2(l);
//var xv = "";
}
});
Console.WriteLine(IntUtility.ltoa2(l));
Test("Int64C++Version2", () => {
for (var i = 0; i < 1000; i++) {
var sx = itoa_int64(l);
//var xv = "";
}
});
Console.WriteLine(itoa_int64(l));
Test("type_to_string_converter_impl_old_int64", () => {
for (var i = 0; i < 1000; i++) {
var sx = type_to_string_converter_impl_old(l);
}
});
Console.WriteLine(type_to_string_converter_impl_old(l));
var culture = CultureInfo.InvariantCulture;
Test("Int64ToString", () => {
for (var i = 0; i < 1000; i++) {
var sx = l.ToString(culture);
}
});
Console.WriteLine(l.ToString());
}
Result:
Iteration: 10000
Completed IntUtilityToInt64_1 in Avg 549 Milliseconds
5181418955
Iteration: 10000
Completed IntUtilityToInt64_2 in Avg 453 Milliseconds
5181418955
Iteration: 10000
Completed Int64C++Version2(NetJSON LongToStr) in Avg 331 Milliseconds
5181418955
Iteration: 10000
Completed type_to_string_converter_impl_old_int64 in Avg 563 Milliseconds
5181418955
Iteration: 10000
Completed Int64ToString in Avg 621 Milliseconds
5181418955
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment