mirror of
https://github.com/PolyhedralDev/Terra.git
synced 2025-07-04 00:45:57 +00:00
create basic terrascript performance test
This commit is contained in:
parent
a02847e3ed
commit
aeadfb21dd
@ -0,0 +1,90 @@
|
|||||||
|
package structure;
|
||||||
|
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.Parser;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.Block;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.ImplementationArguments;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.Returnable.ReturnType;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.Scope;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.functions.Function;
|
||||||
|
import com.dfsek.terra.addons.terrascript.parser.lang.functions.FunctionBuilder;
|
||||||
|
import com.dfsek.terra.addons.terrascript.script.builders.UnaryNumberFunctionBuilder;
|
||||||
|
import com.dfsek.terra.addons.terrascript.script.builders.UnaryStringFunctionBuilder;
|
||||||
|
import com.dfsek.terra.addons.terrascript.tokenizer.Position;
|
||||||
|
|
||||||
|
import net.jafama.FastMath;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
|
||||||
|
public class PerformanceTest {
|
||||||
|
public static void main(String... args) throws IOException {
|
||||||
|
Parser parser = new Parser(
|
||||||
|
IOUtils.toString(PerformanceTest.class.getResourceAsStream("/performance.tesf"), Charset.defaultCharset()));
|
||||||
|
|
||||||
|
parser.registerFunction("assert", new FunctionBuilder<AssertFunction>() {
|
||||||
|
@Override
|
||||||
|
public AssertFunction build(List<Returnable<?>> argumentList, Position position) {
|
||||||
|
return new AssertFunction(position, (Returnable<Boolean>) argumentList.get(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int argNumber() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Returnable.ReturnType getArgument(int position) {
|
||||||
|
return switch(position) {
|
||||||
|
case 0 -> ReturnType.BOOLEAN;
|
||||||
|
default -> null;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}).registerFunction("print",
|
||||||
|
new UnaryStringFunctionBuilder(System.out::println))
|
||||||
|
.registerFunction("sqrt", new UnaryNumberFunctionBuilder(number -> FastMath.sqrt(number.doubleValue())));
|
||||||
|
|
||||||
|
Block block = parser.parse();
|
||||||
|
|
||||||
|
for(int i = 0; i < 20; i++) {
|
||||||
|
long s = System.nanoTime();
|
||||||
|
|
||||||
|
block.apply(null, new Scope());
|
||||||
|
|
||||||
|
long e = System.nanoTime();
|
||||||
|
long d = e - s;
|
||||||
|
|
||||||
|
System.out.println("Took " + ((double) d) / 1000000 + "ms");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static final class AssertFunction implements Function<Void> {
|
||||||
|
private final Position position;
|
||||||
|
private final Returnable<Boolean> arg;
|
||||||
|
|
||||||
|
public AssertFunction(Position position, Returnable<Boolean> arg) {
|
||||||
|
this.position = position;
|
||||||
|
this.arg = arg;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Void apply(ImplementationArguments implementationArguments, Scope scope) {
|
||||||
|
if(!arg.apply(implementationArguments, scope)) throw new IllegalStateException();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Position getPosition() {
|
||||||
|
return position;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ReturnType returnType() {
|
||||||
|
return ReturnType.VOID;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
num epsilon = 0.001;
|
||||||
|
|
||||||
|
for(num n = 1; n < 1000000; n = n + 1) {
|
||||||
|
|
||||||
|
num x = n;
|
||||||
|
|
||||||
|
num root = 0;
|
||||||
|
|
||||||
|
while(true) {
|
||||||
|
root = 0.5 * (x + (n / x));
|
||||||
|
|
||||||
|
num diff = root - x;
|
||||||
|
if(diff < 0) diff = -diff;
|
||||||
|
|
||||||
|
if(diff < epsilon) break;
|
||||||
|
|
||||||
|
x = root;
|
||||||
|
}
|
||||||
|
|
||||||
|
num diff = root - sqrt(n);
|
||||||
|
|
||||||
|
if(diff < 0) diff = -diff;
|
||||||
|
|
||||||
|
assert(diff <= epsilon);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user