Skip to content
Snippets Groups Projects
Commit 74910332 authored by dgelessus's avatar dgelessus
Browse files

Add :time command

parent 5b74f2a9
No related branches found
No related tags found
No related merge requests found
......@@ -29,6 +29,7 @@ import de.prob2.jupyter.commands.LoadFileCommand;
import de.prob2.jupyter.commands.NoSuchCommandException;
import de.prob2.jupyter.commands.PrefCommand;
import de.prob2.jupyter.commands.SolveCommand;
import de.prob2.jupyter.commands.TimeCommand;
import de.prob2.jupyter.commands.VersionCommand;
import io.github.spencerpark.jupyter.kernel.BaseKernel;
......@@ -71,6 +72,7 @@ public final class ProBKernel extends BaseKernel {
this.lineCommands.put(":constants", injector.getInstance(ConstantsCommand.class));
this.lineCommands.put(":initialise", injector.getInstance(InitialiseCommand.class));
this.lineCommands.put(":init", injector.getInstance(InitialiseCommand.class));
this.lineCommands.put(":time", injector.getInstance(TimeCommand.class));
this.lineCommands.put(":groovy", injector.getInstance(GroovyCommand.class));
this.cellCommands = new HashMap<>();
......
package de.prob2.jupyter.commands;
import com.google.inject.Inject;
import de.prob2.jupyter.ProBKernel;
import io.github.spencerpark.jupyter.messages.DisplayData;
import org.jetbrains.annotations.NotNull;
public final class TimeCommand implements LineCommand {
private static final long NANOSECONDS_PER_SECOND = 1000000000L;
@Inject
private TimeCommand() {
super();
}
@Override
public @NotNull String getSyntax() {
return ":time COMMAND [ARGS ...]";
}
@Override
public @NotNull String getShortHelp() {
return "Execute the given command and measure how long it takes to execute.";
}
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String argString) {
final long startTime = System.nanoTime();
final DisplayData result = kernel.eval(argString);
final long stopTime = System.nanoTime();
final long diff = stopTime - startTime;
System.out.printf("Execution time: %d.%09d seconds%n", diff / NANOSECONDS_PER_SECOND, diff % NANOSECONDS_PER_SECOND);
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment