Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (19k points)
How can I measure the speed of code written in Java?

I planning to develop software which will solve Sudoku using all presently available AI and ML algorithms and compare time against the simple brute-force method. I need to measure the time of each algorithm, I would like to ask for suggestions on what is the best way of doing that? Very important, the program must be useful on any machine regardless of CPU power/memory.

Thank you.

1 Answer

0 votes
by (33.1k points)

You can simply use


to calculate deltas of time:

long start = System.currentTimeMillis();

long elapsed = System.currentTimeMillis() - start;

Mind that depending on the operating system you use the precision of the funcion could be greater than 1 millisecond (also tenth of msecs) so you'll have to tweak it to be useful for your analysis.

You can also use System.nanoTime() but you don't have any guarantee that accuracy will be of nanoseconds. Java is kind of interrelated to the aspects used in Machine Learning. Thus, studying Machine Learning Algorithms would be of great use.

Hope this answer helps you!

Browse Categories