命令执行时间在jshell中所花费的时间

huangapple 未分类评论66阅读模式
英文:

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=&quot;&quot;;
for(t0=System.nanoTime(),i=0; i&lt;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&lt;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

huangapple
  • 本文由 发表于 2020年3月15日 20:19:13
  • 转载请务必保留本文链接:https://java.coder-hub.com/60692790.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定