如何多次调用Java文件的Main函数?
请我需要多次调用 java 文件的 main 函数,例如(100)次,并测量它完成所需的时间。目的是将时间与其他一些文件进行比较。 [从字面上讲,我正在实施算法]。
我知道计算时间可以通过以下方式实现:
long startTime = System.currentTimeMillis();
GetExecutionTimes ext=new GetExecutionTimes();
ext.callMethod();
long endTime = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"
+ (endTime-startTime));
*其中 ("callMethod") 是我的方法的名称。
但是,如果我有一个完整的 java 文件并且我想从另一个类(或文件)在计算其执行时间时,该怎么做?
总结:
任务 1:从另一个类多次调用 java 文件的 main 函数。
任务 2:计算执行(任务 1)所需的时间。
请为完成任务 1 和 2 提供任何建议或想法,我们将不胜感激。
预先感谢您的帮助,
Please i need to call the main function of a java file several times like (100) times and measure the time it takes for it to complete. The objective is comparing the time with some other files. [literally speaking, I'm Implementing Algorithms].
I know that calculating the time can be achieved by:
long startTime = System.currentTimeMillis();
GetExecutionTimes ext=new GetExecutionTimes();
ext.callMethod();
long endTime = System.currentTimeMillis();
System.out.println("Total elapsed time in execution of method callMethod() is :"
+ (endTime-startTime));
*Where ("callMethod") is the name of my method.
However, if i have a complete java file and i want to call its main function from another class (or file) while calculating its execution time, what's the way to do that?
In Summary:
Task 1: Calling the main function of a java file several times from another class.
Task 2: Calculating the time it takes to execute (Task 1).
Please any suggestions or ideas for accomplishing tasks 1 and 2 are greatly appreciated.
Thanks in advance for your help,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想多次调用它,我会使用循环。您可以多次调用
main(String[])
方法,但是最好调用底层算法。If you want to call it several times, I would use a loop. You can call a
main(String[])
method several times, however you may be better off calling the underlying algo.如果没有很好地理解 JVM、JIT、垃圾收集以及不同的非程序线程如何混淆您的基准测试,请注意进行此类基准测试。
您可以收集良好的基准测试,但这样做最终会成为统计挑战,因为测量结果相当嘈杂(由于 JVM 时不时地调度垃圾收集,JVM 会暂停将代码编译为本机指令,并且本机指令通常运行速度比非本地快得多)。
也就是说,您可以像调用任何其他静态方法一样调用静态 main 方法。请注意,如果您确实想要进行“相同”调用,则可能需要存储 args 数组。
Beware of doing such benchmarks without a good understanding of the JVM, JIT, garbage collection, and how the differing non-program threads can confound your benchmarking.
You can collect good benchmarks, but doing so eventually becomes a statistical challenge, as the measurements are rather noisy (due to the JVM scheduling garbage collection every now and then, the JVM pausing to compile your code to native instructions, and the native instructions generally running much faster than non-native).
That said, you can call the static main method just like any other static method. Note that you probably need to store off the args array if you really want to have the "same" call.
您可以尝试创建一个 JUnit 测试用例并在运行程序中调用您的test 的方法(内部将调用实际对象的 main 方法)。这样您就可以通过 JUnit 获取执行时间。
Can you try with creating a JUnit test case and in the runner call your test's method (which internally will call the actual object's main). That way you might get the execution times via JUnit.
main() 只是类中的公共静态方法,因此您始终可以像这样调用它
main() is just a public, static method in a class, so you can always just call it like