Created
November 19, 2012 08:34
-
-
Save angelsl/4109585 to your computer and use it in GitHub Desktop.
NX Benchmarks
This file contains hidden or 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
// reNX is copyright angelsl, 2011 to 2012 inclusive. | |
// | |
// This file is part of reNX. | |
// | |
// reNX is free software: you can redistribute it and/or modify | |
// it under the terms of the GNU General Public License as published by | |
// the Free Software Foundation, either version 3 of the License, or | |
// (at your option) any later version. | |
// | |
// reNX is distributed in the hope that it will be useful, | |
// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
// GNU General Public License for more details. | |
// | |
// You should have received a copy of the GNU General Public License | |
// along with reNX. If not, see <http://www.gnu.org/licenses/>. | |
using MapleLib.WzLib; | |
using reNX; | |
using reNX.NXProperties; | |
using reWZ; | |
using reWZ.WZProperties; | |
using System; | |
using System.Diagnostics; | |
namespace NXWZBenchmark | |
{ | |
internal static class Program | |
{ | |
private static WZFile wz = new WZFile("PKG1.wz", WZVariant.Classic, false); | |
private static WZObject wzn; | |
private static NXFile nx = new NXFile("PKG4.nx"); | |
private static NXNode nxn; | |
private static WzFile ml = new WzFile("PKG1.wz", WzMapleVersion.CLASSIC); | |
private static IWzImageProperty mln; | |
private static void Main(string[] args) | |
{ | |
int count = args.Length > 1 ? int.Parse(args[1]) : 1; | |
for (int i = 0; i < count; ++i) | |
switch (args[0].ToLower()) { | |
case "renx": | |
Test("NX Ld", reNXLoad, 0x1000); | |
nxn = nx.BaseNode["Map"]["Map"]["Map1"]["105060000.img"]["1"]["tile"]; | |
Test("NX SS", reNXStringSearch, 0x1000); | |
Test("NX PR", reNXParseAndRecurse, 0x10); | |
Test("NX Re", reNXRecurse, 0x10); | |
break; | |
case "rewz": | |
Test("WZ Ld", reWZLoad, 0x10); | |
wzn = wz.MainDirectory["Map"]["Map"]["Map1"]["105060000.img"]["1"]["tile"]; | |
Test("WZ SS", reWZStringSearch, 0x10); | |
Test("WZ PR", reWZParseAndRecurse, 1); | |
Test("WZ Re", reWZRecurse, 0x10); | |
break; | |
case "ml": | |
ml.ParseWzFile(); | |
Test("ML Ld", MLLoad, 3); | |
mln = ((IWzImageProperty)((IPropertyContainer)((IPropertyContainer)((WzDirectory)((WzDirectory)((WzDirectory)ml.WzDirectory["Map"])["Map"])["Map1"])["105060000.img"])["1"])["tile"]); | |
Test("ML SS", MLStringSearch, 3); | |
Test("ML PR", MLParseAndRecurse, 1); | |
Test("ML Re", MLRecurse, 3); | |
break; | |
} | |
} | |
private static void reNXLoad() | |
{ | |
nx.Dispose(); | |
nx = new NXFile("PKG4.nx"); | |
} | |
private static void reWZLoad() | |
{ | |
wz.Dispose(); | |
wz = new WZFile("PKG1.wz", WZVariant.Classic, false); | |
} | |
private static void reNXParseAndRecurse() | |
{ | |
NXFile nz = new NXFile("PKG4.nx"); | |
reNXRecurseHelper(nz.BaseNode); | |
} | |
private static void reNXRecurse() | |
{ | |
reNXRecurseHelper(nx.BaseNode); | |
} | |
private static void reNXRecurseHelper(NXNode n) | |
{ | |
foreach(NXNode c in n) | |
reNXRecurseHelper(c); | |
} | |
private static void reWZParseAndRecurse() | |
{ | |
WZFile wx = new WZFile("PKG1.wz", WZVariant.Classic, false); | |
reWZRecurseHelper(wx.MainDirectory); | |
} | |
private static void reWZRecurse() | |
{ | |
reWZRecurseHelper(wz.MainDirectory); | |
} | |
private static void reWZRecurseHelper(WZObject n) | |
{ | |
foreach (WZObject c in n) | |
reWZRecurseHelper(c); | |
} | |
private static void reNXStringSearch() | |
{ | |
foreach (NXNode c in nxn) | |
if (nxn[c.Name] != c) throw new InvalidOperationException(); | |
} | |
private static void reWZStringSearch() | |
{ | |
foreach (WZObject c in wzn) | |
if (wzn[c.Name] != c) throw new InvalidOperationException(); | |
} | |
private static void MLLoad() | |
{ | |
ml.Dispose(); | |
ml = new WzFile("PKG1.wz", WzMapleVersion.CLASSIC); | |
ml.ParseWzFile(); | |
} | |
private static void MLStringSearch() | |
{ | |
foreach(IWzImageProperty c in mln.WzProperties) | |
if (mln[c.Name] != c) throw new InvalidOperationException(); | |
} | |
private static void MLParseAndRecurse() | |
{ | |
WzFile mm = new WzFile("PKG1.wz", WzMapleVersion.CLASSIC); | |
mm.ParseWzFile(); | |
MLRecurseHelper(mm.WzDirectory); | |
} | |
private static void MLRecurse() | |
{ | |
MLRecurseHelper(ml.WzDirectory); | |
} | |
private static void MLRecurseHelper(WzDirectory c) | |
{ | |
foreach(WzDirectory d in c.WzDirectories) | |
MLRecurseHelper(d); | |
foreach(WzImage d in c.WzImages) | |
MLRecurseHelper(d); | |
} | |
private static void MLRecurseHelper(WzImage c) | |
{ | |
foreach(IWzImageProperty i in c.WzProperties) | |
MLRecurseHelper(i); | |
} | |
private static void MLRecurseHelper(IWzImageProperty i) | |
{ | |
if (i.WzProperties == null) return; | |
foreach(IWzImageProperty x in i.WzProperties) | |
MLRecurseHelper(x); | |
} | |
private static void Test(string name, Action c, int bestOf) | |
{ | |
double bestms = Double.PositiveInfinity; | |
Stopwatch t = new Stopwatch(); | |
for (int x = 0; x < bestOf; ++x) | |
{ | |
t.Reset(); | |
t.Start(); | |
c(); | |
t.Stop(); | |
bestms = Math.Min(bestms, t.Elapsed.TotalMilliseconds); | |
} | |
Console.WriteLine("{0}: {1}µs", name, bestms*1000); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment