如何多次调用Java文件的Main函数?

发布于 2024-11-06 22:50:45 字数 697 浏览 8 评论 0原文

请我需要多次调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

自我难过 2024-11-13 22:50:45

如果你想多次调用它,我会使用循环。您可以多次调用 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.

情痴 2024-11-13 22:50:45

如果没有很好地理解 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.

情泪▽动烟 2024-11-13 22:50:45

您可以尝试创建一个 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.

屋檐 2024-11-13 22:50:45

main() 只是类中的公共静态方法,因此您始终可以像这样调用它

for (int i = 0; i < 100; i++) {
    MyClassWithMain.main(null);
}

main() is just a public, static method in a class, so you can always just call it like

for (int i = 0; i < 100; i++) {
    MyClassWithMain.main(null);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文