Skip to content

Commit

Permalink
- deserialize optimization test
Browse files Browse the repository at this point in the history
  • Loading branch information
mgholam committed Aug 5, 2018
1 parent ca87c04 commit 95803cc
Show file tree
Hide file tree
Showing 15 changed files with 625 additions and 456 deletions.
120 changes: 120 additions & 0 deletions Consoletest/Program.cs
Expand Up @@ -5,10 +5,44 @@
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;

namespace consoletest
{
public class TestClass
{
public string Channel { get; set; }
public string Start { get; set; }
public string Stop { get; set; }
public string Eventid { get; set; }
public string Charset { get; set; }

public List<string> Titles { get; set; } = new List<string>();
public List<string> Events { get; set; } = new List<string>();
public List<string> Descriptions { get; set; } = new List<string>();

public static List<TestClass> CreateList(int count)
{
List<TestClass> lst = new List<TestClass>();
foreach (int i in Enumerable.Range(1, count))
{
TestClass t = new TestClass
{
Channel = $"Channel-{i % 10}",
Start = $"{i * 1000}",
Stop = $"{i * 1000 + 10}",
Charset = "255"
};
t.Descriptions.Add($"Description Description Description Description Description Description Description {i} ");
t.Events.Add($"Event {i} ");
t.Titles.Add($"The Title {i} ");
lst.Add(t);
}
return lst;
}
}

public class Program
{
static int count = 1000;
Expand All @@ -22,8 +56,94 @@ public class Program
// //public int i = 0;
//}

private static void fastjson_deserialize(int count)
{
Console.WriteLine();
Console.WriteLine("fastjson deserialize");
List<double> times = new List<double>();
var data = TestClass.CreateList(20000);
string jsonText = JSON.ToJSON(data, new fastJSON.JSONParameters { UseExtensions = false });
//File.WriteAllText("FastJson.json", jsonText);
for (int tests = 0; tests < count; tests++)
{
DateTime st = DateTime.Now;
var result = JSON.ToObject<List<TestClass>>(jsonText);
times.Add(DateTime.Now.Subtract(st).TotalMilliseconds);
if (tests % 10 == 0)
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine($"Min: {times.Min()} Max: {times.Max()} Average: {times.Average()}");

}

private static void newton_deserialize(int count)
{
Console.WriteLine();
Console.WriteLine("newton deserialize");
List<double> times = new List<double>();
var data = TestClass.CreateList(20000);
string jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(data);
//File.WriteAllText("Newton.json", jsonText);
for (int tests = 0; tests < count; tests++)
{
DateTime st = DateTime.Now;
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TestClass>>(jsonText);
times.Add(DateTime.Now.Subtract(st).TotalMilliseconds);
if (tests % 10 == 0)
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine($"Min: {times.Min()} Max: {times.Max()} Average: {times.Average()}");
}


private static void fastjson_serialize(int count)
{
Console.WriteLine();
Console.WriteLine("fastjson serialize");
List<double> times = new List<double>();
var data = TestClass.CreateList(20000);
for (int tests = 0; tests < count; tests++)
{
DateTime st = DateTime.Now;
string jsonText = JSON.ToJSON(data, new fastJSON.JSONParameters { UseExtensions = false });

times.Add(DateTime.Now.Subtract(st).TotalMilliseconds);
if (tests % 10 == 0)
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine($"Min: {times.Min()} Max: {times.Max()} Average: {times.Average()}");

}

private static void newton_serialize(int count)
{
Console.WriteLine();
Console.WriteLine("newton serialize");
List<double> times = new List<double>();
var data = TestClass.CreateList(20000);
for (int tests = 0; tests < count; tests++)
{
DateTime st = DateTime.Now;
string jsonText = Newtonsoft.Json.JsonConvert.SerializeObject(data);
times.Add(DateTime.Now.Subtract(st).TotalMilliseconds);
if (tests % 10 == 0)
Console.Write(".");
}
Console.WriteLine();
Console.WriteLine($"Min: {times.Min()} Max: {times.Max()} Average: {times.Average()}");
}


public static void Main(string[] args)
{
//fastjson_serialize(100);
//newton_serialize(100);
fastjson_deserialize(10);
//newton_deserialize(10);
return;
//string s = "{ \"Section1\" : { \"Key1\" : \"Value1\", \"Key2\" : \"Value2\", \"Key3\" : \"Value3\", \"Key4\" : \"Value4\", \"Key5\" : \"Value5\" } }";
//var oo = JSON.ToDynamic(s);

Expand Down
4 changes: 4 additions & 0 deletions Consoletest/consoletest.csproj
Expand Up @@ -57,6 +57,10 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Microsoft.CSharp" />
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>.\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
Expand Down
4 changes: 2 additions & 2 deletions UnitTests/Tests.cs
Expand Up @@ -1271,7 +1271,7 @@ internal static object FastCreateInstance(Type objtype)
{
if (objtype.IsClass)
{
DynamicMethod dynMethod = new DynamicMethod("_", objtype, null);
DynamicMethod dynMethod = new DynamicMethod("_fcc", objtype, null, true);
ILGenerator ilGen = dynMethod.GetILGenerator();
ilGen.Emit(OpCodes.Newobj, objtype.GetConstructor(Type.EmptyTypes));
ilGen.Emit(OpCodes.Ret);
Expand All @@ -1280,7 +1280,7 @@ internal static object FastCreateInstance(Type objtype)
}
else // structs
{
DynamicMethod dynMethod = new DynamicMethod("_", typeof(object), null);
DynamicMethod dynMethod = new DynamicMethod("_fcs", typeof(object), null, true);
ILGenerator ilGen = dynMethod.GetILGenerator();
var lv = ilGen.DeclareLocal(objtype);
ilGen.Emit(OpCodes.Ldloca_S, lv);
Expand Down
45 changes: 32 additions & 13 deletions fastJSON/fastJSON net35.csproj → fastJSON 3.5/fastJSON 3.5.csproj
Expand Up @@ -77,24 +77,14 @@
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>fastJSON.snk</AssemblyOriginatorKeyFile>
<AssemblyOriginatorKeyFile>
</AssemblyOriginatorKeyFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="AssemblyInfo.cs" />
<Compile Include="dynamic.cs" />
<Compile Include="Getters.cs" />
<Compile Include="Formatter.cs" />
<Compile Include="JsonSerializer.cs" />
<Compile Include="JSON.cs" />
<Compile Include="JsonParser.cs" />
<Compile Include="Reflection.cs" />
<Compile Include="SafeDictionary.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
Expand All @@ -118,7 +108,36 @@
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="fastJSON.snk" />
<Compile Include="..\fastJSON\AssemblyInfo.cs">
<Link>AssemblyInfo.cs</Link>
</Compile>
<Compile Include="..\fastJSON\dynamic.cs">
<Link>dynamic.cs</Link>
</Compile>
<Compile Include="..\fastJSON\Formatter.cs">
<Link>Formatter.cs</Link>
</Compile>
<Compile Include="..\fastJSON\Getters.cs">
<Link>Getters.cs</Link>
</Compile>
<Compile Include="..\fastJSON\Helper.cs">
<Link>Helper.cs</Link>
</Compile>
<Compile Include="..\fastJSON\JSON.cs">
<Link>JSON.cs</Link>
</Compile>
<Compile Include="..\fastJSON\JsonParser.cs">
<Link>JsonParser.cs</Link>
</Compile>
<Compile Include="..\fastJSON\JsonSerializer.cs">
<Link>JsonSerializer.cs</Link>
</Compile>
<Compile Include="..\fastJSON\Reflection.cs">
<Link>Reflection.cs</Link>
</Compile>
<Compile Include="..\fastJSON\SafeDictionary.cs">
<Link>SafeDictionary.cs</Link>
</Compile>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
2 changes: 1 addition & 1 deletion fastJSON.nuspec
Expand Up @@ -2,7 +2,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>fastJSON</id>
<version>2.1.34.0</version>
<version>2.1.35.0</version>
<title>fastJSON</title>
<authors>mgholam</authors>
<owners />
Expand Down
4 changes: 3 additions & 1 deletion fastJSON.sln
Expand Up @@ -9,7 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fastJSON", "fastJSON\fastJS
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{F4BFF0D2-45F6-413B-B1E7-BC4A0D6596FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fastJSON net35", "fastJSON\fastJSON net35.csproj", "{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "fastJSON 3.5", "fastJSON 3.5\fastJSON 3.5.csproj", "{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down Expand Up @@ -44,9 +44,11 @@ Global
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Debug|x86.ActiveCfg = Debug|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Debug|x86.Build.0 = Debug|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Release|Any CPU.Build.0 = Release|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Release|x86.ActiveCfg = Release|Any CPU
{FDAF5695-E651-4D4D-8E15-0BD9FBEF5069}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 1 addition & 2 deletions fastJSON/AssemblyInfo.cs
@@ -1,11 +1,10 @@
using System.Reflection;


[assembly: AssemblyTitle("fastJSON")]
[assembly: AssemblyDescription("smallest fastest polymorphic json serializer")]
[assembly: AssemblyProduct("fastJSON")]
[assembly: AssemblyCopyright("2010-2018")]


[assembly: AssemblyVersion("2.1.0.0")]
[assembly: AssemblyFileVersion("2.1.34.0")]
[assembly: AssemblyFileVersion("2.1.35.0")]

0 comments on commit 95803cc

Please sign in to comment.