英文:
Command execution elapsed time in jshell
问题
我寻找了一个选项,可以让jshell报告命令的执行时间,但是我没有找到任何内容。
那么我的问题是,在jshell中提交命令进行执行时,有什么最佳方法可以报告经过的时间?
示例
要检查执行 100,000 次字符串连接所需的时间(例如与 StringBuffer.append()
进行比较)
我的基本解决方案是:
/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;
在这里,jshell 在一个完整的步骤中执行循环,并在完成所有迭代后打印经过的时间。
然后与另一种解决方案进行比较(使用 StringBuilder)
/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;
有没有更好、更清晰、更智能的方法?
附注:我知道通过这种方法得到的测量结果并不准确,不过这是一种快速获取大致概念的方法。
英文:
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
专注分享java语言的经验与见解,让所有开发者获益!
评论