Skip to content

Instantly share code, notes, and snippets.

@angelsl
Created November 19, 2012 08:35
Show Gist options
  • Save angelsl/4109587 to your computer and use it in GitHub Desktop.
Save angelsl/4109587 to your computer and use it in GitHub Desktop.
jwzlib benchmark
// 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/>.
import wz.WzDirectory;
import wz.WzFile;
import wz.WzImage;
import wz.WzObject;
import wz.io.WzInputStream;
import wz.tool.WzKeyGenerator;
import java.io.FileInputStream;
public class Benchmark {
private static WzFile f;
private static WzFile makeWz() {
try {
WzFile f = new WzFile("Data.wz");
FileInputStream fis = new FileInputStream("PKG1.wz");
byte[] key = new byte[0x10000];
WzInputStream wis = new WzInputStream(fis, key);
f.parse(wis);
return f;
} catch (Throwable t) {
throw new RuntimeException(t);
}
}
public static void main(String[] args) {
test("JZ Ld", new Runnable() {
@Override
public void run() {
bLoad();
}
}, 5);
test("JZ SS", new Runnable() {
@Override
public void run() {
bStringSearch();
}
}, 0x10);
test("JZ PR", new Runnable() {
@Override
public void run() {
bParseAndRecurse();
}
}, 2);
test("JZ Re", new Runnable() {
@Override
public void run() {
bRecurse();
}
}, 2);
}
private static void test(String name, Runnable r, int bestOf) {
System.out.print(name + ": ");
double best = Double.POSITIVE_INFINITY;
for (int x = 0; x < bestOf; ++x) {
long start = System.nanoTime();
r.run();
long end = System.nanoTime();
best = Math.min(best, (end - start) / 1000D);
}
System.out.println(best + "µs");
}
private static void bLoad() {
f = makeWz();
}
private static void bStringSearch() {
//["Map"]["Map"]["Map1"]["105060000.img"]["1"]["tile"]
WzObject wo = f.getRootDirectory().getDirectory("Map").getDirectory("Map").getDirectory("Map1").getImage("105060000.img")
.getChild("1").getChild("tile");
for(WzObject wi : wo)
{
if(wo.getChild(wi.getName()) != wi) throw new RuntimeException();
}
}
private static void bParseAndRecurse() {
WzFile z = makeWz();
bRecurseHelper(z.getRootDirectory());
}
private static void bRecurse()
{
bRecurseHelper(f.getRootDirectory());
}
private static void bRecurseHelper(WzDirectory w)
{
for(WzDirectory d : w.getDirectories())
bRecurseHelper(d);
for(WzImage i : w.getImages())
bRecurseHelper(i);
}
private static void bRecurseHelper(WzImage i)
{
for(WzObject c : i)
bRecurseHelper(c);
}
private static void bRecurseHelper(WzObject c)
{
for(WzObject i : c)
bRecurseHelper(i);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment