1

I looked for an option to have jshell report the execution time of a command, but I could not find any.

Then my question is what is the best way to have elapsed time reported when submitting a command for execution in jshell?

Example

To check the time required to perform 100k string concatenations (e.g. to compare to StringBuffer.append())

My basic solution is:

/reset
/set feedback verbose
int i;
long t0;
String s="";
for(t0=System.nanoTime(),i=0; i<100000; ++i){
s+=i;
} long elapsed=System.nanoTime()-t0;

Where jshell executes the loop in a whole single step and prints the elapsed time after completing all iterations.

Then to compare to a different solution (using StringBuffer)

/reset
/set feedback verbose
int i;
long t0;
StringBuilder b=new StringBuilder();
for(t0=System.nanoTime(),i=0; i<100000; ++i){
b.append(i);
} long elapsed=System.nanoTime()-t0;

Is there a better, cleaner, smarter way?


PS: I know the measure you get with this approach is not accurate, though this is a quick way to get a rough idea

Marco Torchiano
  • 712
  • 7
  • 21

0 Answers0