Skip to content

Instantly share code, notes, and snippets.

@angelsl
Created July 23, 2012 12:27
Show Gist options
  • Save angelsl/3163349 to your computer and use it in GitHub Desktop.
Save angelsl/3163349 to your computer and use it in GitHub Desktop.
NX Thread reNX/reWZ/MapleLib2 Benchmark
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using MapleLib.WzLib;
using MapleLib.WzLib.WzProperties;
using reNX;
using reNX.NXProperties;
using reWZ;
namespace NXWZBenchmark
{
internal static class Program
{
private static Stopwatch t = new Stopwatch();
private static void Main(string[] args)
{
int count = args.Length > 1 ? int.Parse(args[1]) : 5;
for (int i = 0; i < count; ++i)
switch (args[0].ToLower()) {
case "renx":
reNXBenchmark();
break;
case "rewz":
reWZBenchmark();
break;
case "ml":
MapleLib2Benchmark();
break;
}
}
private static void SOS(string format, decimal factor = 1M)
{
t.Stop();
Console.WriteLine(format, (decimal)t.Elapsed.Ticks/TimeSpan.TicksPerMillisecond*factor);
t.Restart();
}
private static void MapleLib2Benchmark()
{
WzFile file = new WzFile(@"D:\Misc\Applications\WZ\GMSv40b\Data.wz", WzMapleVersion.CLASSIC);
file.ParseWzFile();
t.Stop();
t.Reset();
file.Dispose();
t.Start();
file = new WzFile(@"D:\Misc\Applications\WZ\GMSv40b\Data.wz", WzMapleVersion.CLASSIC);
file.ParseWzFile();
t.Stop();
SOS("Load: {0} µs", 1000);
for (int i = 0; i < 20000000; ++i) {
WzDirectory n1 = (WzDirectory)file["Effect"];
WzImage n2 = (WzImage)n1["BasicEff.img"];
WzSubProperty n3 = (WzSubProperty)n2["LevelUp"];
WzCanvasProperty n4 = (WzCanvasProperty)n3["5"];
WzVectorProperty n5 = (WzVectorProperty)n4["origin"];
}
SOS("Access: {0} ms", 0.05M);
Recurse(file.WzDirectory);
SOS("Recurse: {0} ms");
Recurse(file.WzDirectory);
SOS("2nd Recurse: {0} ms");
PrintMemoryGC();
file.Dispose();
file = null;
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private static void StandardBenchmarkRecurse(WZObject n)
{
foreach (WZObject c in n) StandardBenchmarkRecurse(c);
}
private static void reWZBenchmark()
{
byte[] file = File.ReadAllBytes(@"D:\Misc\Applications\WZ\GMSv40b\Data.wz");
MemoryStream ms = new MemoryStream(file, false);
WZFile wz = new WZFile(ms, WZVariant.Classic, false);
t.Stop();
t.Reset();
wz.Dispose();
t.Start();
wz = new WZFile(ms, WZVariant.Classic, false);
t.Stop();
SOS("Load: {0} µs", 1000);
for (int i = 0; i < 20000000; ++i) {
Point p = ((WZProperty<Point>)wz.MainDirectory["Effect"]["BasicEff.img"]["LevelUp"]["5"]["origin"]).Value;
}
SOS("Access: {0} ms", 0.05M);
StandardBenchmarkRecurse(wz.MainDirectory);
SOS("Recurse: {0} ms");
StandardBenchmarkRecurse(wz.MainDirectory);
SOS("2nd Recurse: {0} ms");
PrintMemoryGC();
wz.Dispose();
wz = null;
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private static void StandardBenchmarkRecurse(NXNode n)
{
foreach (NXNode c in n) StandardBenchmarkRecurse(c);
}
private static void reNXBenchmark()
{
byte[] file = File.ReadAllBytes(@"D:\Data.nx");
NXFile wz = new NXFile(file);
t.Stop();
t.Reset();
wz.Dispose();
t.Start();
wz = new NXFile(file);
t.Stop();
SOS("Load: {0} µs", 1000);
for (int i = 0; i < 20000000; ++i) {
NXNode n = wz.BaseNode["Effect"]["BasicEff.img"]["LevelUp"]["5"]["origin"];
}
SOS("Access: {0} ms", 0.05M);
StandardBenchmarkRecurse(wz.BaseNode);
SOS("Recurse: {0} ms");
StandardBenchmarkRecurse(wz.BaseNode);
SOS("2nd Recurse: {0} ms");
PrintMemoryGC();
wz.Dispose();
wz = null;
GC.Collect();
GC.Collect();
GC.WaitForPendingFinalizers();
}
private static void Recurse(WzDirectory obj)
{
foreach (WzDirectory o in obj.WzDirectories) Recurse(o);
foreach (WzImage o in obj.WzImages) Recurse(o);
}
private static void Recurse(WzImage obj)
{
foreach (IWzImageProperty o in obj.WzProperties) Recurse(o);
}
private static void Recurse(IWzImageProperty obj)
{
if (obj.WzProperties == null) return;
foreach (IWzImageProperty o in obj.WzProperties) Recurse(o);
}
[DllImport("psapi.dll", SetLastError = true)]
private static extern bool GetProcessMemoryInfo(IntPtr hProcess, out PROCESS_MEMORY_COUNTERS counters, uint size);
[DllImport("kernel32.dll")]
private static extern IntPtr GetCurrentProcess();
private static void PrintMemoryGC()
{
PROCESS_MEMORY_COUNTERS pmc;
GetProcessMemoryInfo(GetCurrentProcess(), out pmc, 40);
Console.WriteLine("Memory: {0} kb, {1} kb, {2} kb", (long)pmc.PeakPagefileUsage/1000, (long)pmc.PeakWorkingSetSize/1000, Environment.WorkingSet/1000);
}
#region Nested type: PROCESS_MEMORY_COUNTERS
[StructLayout(LayoutKind.Sequential, Size = 40)]
private struct PROCESS_MEMORY_COUNTERS
{
public uint cb;
public uint PageFaultCount;
public IntPtr PeakWorkingSetSize;
public IntPtr WorkingSetSize;
public IntPtr QuotaPeakPagedPoolUsage;
public IntPtr QuotaPagedPoolUsage;
public IntPtr QuotaPeakNonPagedPoolUsage;
public IntPtr QuotaNonPagedPoolUsage;
public IntPtr PagefileUsage;
public IntPtr PeakPagefileUsage;
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment