Skip to content
Snippets Groups Projects
Select Git revision
  • 421dab0e3d59cf24b01f2ebbc5e1bd7a0efc0bc4
  • master default protected
  • exec_auto_adjust_trace
  • let_variables
  • v1.4.1
  • v1.4.0
  • v1.3.0
  • v1.2.0
  • v1.1.0
  • v1.0.0
10 results

TimeCommand.java

Blame
  • user avatar
    dgelessus authored
    Completion is currently only supported for command names, and for
    arguments of the :help and :time commands.
    421dab0e
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    TimeCommand.java 1.44 KiB
    package de.prob2.jupyter.commands;
    
    import com.google.inject.Inject;
    
    import de.prob2.jupyter.ProBKernel;
    
    import io.github.spencerpark.jupyter.kernel.ReplacementOptions;
    import io.github.spencerpark.jupyter.kernel.display.DisplayData;
    
    import org.jetbrains.annotations.NotNull;
    import org.jetbrains.annotations.Nullable;
    
    public final class TimeCommand implements Command {
    	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 @Nullable 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;
    		final String text = String.format("Execution time: %d.%09d seconds", diff / NANOSECONDS_PER_SECOND, diff % NANOSECONDS_PER_SECOND);
    		final DisplayData timeDisplay = new DisplayData(text);
    		timeDisplay.putMarkdown(text);
    		kernel.display(timeDisplay);
    		return result;
    	}
    	
    	@Override
    	public @Nullable ReplacementOptions complete(final @NotNull ProBKernel kernel, final @NotNull String argString, final int at) {
    		return kernel.complete(argString, at);
    	}
    }