Recently, tried to find elapsed time to parse Json string's to corresponding C# objects using NewtonSoft API.
I am having my own Json parser code to convert Json string's to C# object too.
So, thought of comparing the time taken by these two stuffs.
Here is, what I've tried:-
Ex1:-
for(int i=0;i<2;i++)
{
var inputText = File.ReadAllText("TextFile1.txt");
var st = DateTime.Now;
var obj = JsonConvert.DeserializeObject<Text1Root>(inputText);
var end = DateTime.Now;
Console.WriteLine("NewtonJsoft: " + (end - st).Milliseconds);
var st1 = DateTime.Now;
var generatedObj = ConvertJsonStrToClassStructure<Text1Root>
(inputText); //My parser code
var en1 = DateTime.Now;
Console.WriteLine("Custom: " + (en1 - st1).Milliseconds);
}
Generated output:-
Ex2:-
var inputText = File.ReadAllText("TextFile1.txt");
Console.WriteLine("For TextFile1.txt");
var st = DateTime.Now;
dynamic obj = JsonConvert.DeserializeObject<Text1Root>(inputText);
var end = DateTime.Now;
Console.WriteLine("NewtonJsoft: " + (end - st).Milliseconds);
var st1 = DateTime.Now;
dynamic generatedObj = ConvertJsonStrToClassStructure<Text1Root>
(inputText);
var en1 = DateTime.Now;
Console.WriteLine("Custom: " + (en1 - st1).Milliseconds);
inputText = File.ReadAllText("TextFile2.txt");
Console.WriteLine("For TextFile2.txt");
st = DateTime.Now;
obj = JsonConvert.DeserializeObject<Text2Root>(inputText);
end = DateTime.Now;
Console.WriteLine("NewtonJsoft: " + (end - st).Milliseconds);
st1 = DateTime.Now;
generatedObj = ConvertJsonStrToClassStructure<Text2Root>(inputText);
en1 = DateTime.Now;
Console.WriteLine("Custom: " + (en1 - st1).Milliseconds);
Generated output:-
Ex3:-
var st = DateTime.Now;
var obj = JsonConvert.DeserializeObject<Text1Root>(inputText);
var end = DateTime.Now;
Console.WriteLine("NewtonJsoft: " + (end - st).Milliseconds);
Generated output:-
For Ex1:-
For the 1st run of JsonConvert.DeserializeObject<>(""), it is taking some time to process, but in the next run
in the loop, it took nearly 0 m sec. Is it using any kind of cache internally? I tried looking the MemCache object. But unfortunately nothing was there.
For Ex2:-
For processing the TextFile1.txt, it took some time. But while processing the TextFile2.txt, it took very minimal time.
For Ex3:-
For testing, I have only NewtonSoft code to parse. And this time, I restarted the application each and every time, to check whether NewtonSoft is actually taking negligible time to parse the input. But in each and every run, it took some time like some 150 m sec (app) to parse.
So, I am left with no clue on,
1. How it is parsing the string to C# in negligible time in successive run
2. How it is taking some time for parsing in first run/ or how it is taking time in parsing for each restart of the application.
3. Whether NewtonSoft is using any other stuffs like caching internally. If so,
is there any way to turn off this behaviour.
Could any one suggest some inputs on this one.
Thanks.