This question already has answers here:
Measure execution time for a Java method [duplicate]
(8 answers)
How do I write a correct micro-benchmark in Java?
(11 answers)
Closed 3 years ago.
Im C++ coder but somehow today I ended with Java, so I did little project with some sorting algorithms (I made them into classes) and I wonder, is it possible to println passed time of calculation of a class ? Not whole project but only one class ? So I can compare theese algorithms.
Thank you for any help.
You can get the current time at the beginning and at the end of the code, and subtract them:
long s = System.nanoTime();
//Your code;
long elapsed = System.nanoTime() - s;
Your time elapsed will be in nanoseconds, but it's an easy conversion.
Related
This question already has answers here:
How to find out the number of CPUs using python
(15 answers)
Closed 3 years ago.
In Java, this will return the number of available processors:
Runtime.getRuntime().availableProcessors()
This is handy when deciding how many long running threads to create.
Is there an equivalent function to call in Python?
Here you go, there are definitely duplicate answers out there
import os
os.cpu_count()
With python 2.6 or greater ,
multiprocessing.cpu_count()
This question already has answers here:
Fuzzy string search library in Java [closed]
(8 answers)
Closed 6 years ago.
I am working on a game where the user has to fill in a name of a celebrity. If the name is not 100% correct but nearly correct, the compare should succeed. Is there a ready to use function in java or something someone ever has written so I can use it ?
What you are looking for is the Levenshtein algortithm
You'll find here some Java implementations : https://en.wikibooks.org/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#Java
Or, if you don't want/need to understand how it works, you can get the score directly from Apache StringUtils : getLevenshteinDistance
And if you want to get the percentage of similarities, you can do :
int lev = StringUtils.getLevenshteinDistance(s1, s2);
double ratio = ((double) lev) / (Math.max(s1.length, s2.length));
This question already has an answer here:
Converting the format of the date in java
(1 answer)
Closed 1 year ago.
If I do:
ZonedDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE)
It returns a String, rather I'd like it to return a ZonedDateTime object. Is there a clean way of doing that. This is the obvious solution:
ZonedDateTime.parse(ZonedDateTime.now().format(DateTimeFormatter.BASIC_ISO_DATE));
Is there an easier way to do that? That just seems a little too verbose.
just use ZonedDateTime.now()
source: https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#now--
This question already has answers here:
Countdowntimer in minutes and seconds
(4 answers)
Closed 8 years ago.
i am sorry for asking question that already answered in other post.
i already have tick method running each second, since i don't know about time format, i only display counter (int variable type) from 1800 (30 minutes) , subtract it one (sec) by each tick , until 0.
can you teach me how to make it to display 30:00, 29:59 .. instead of 1800,1799 until 00:00 ..?
if possible, i don't want to implement any other method to get it done. if you have any idea how to make this possible as simple as possible.. e.g by using time or date type
thank you.
You need to use the timedelta() method from the datetime module. You can use it as follows :
import datetime
your_nicely_formated_countdown_value = datetime.timedelta(seconds=your_current_tick_value))
For instance,
import datetime
print(str(datetime.timedelta(seconds=300)))
# => '0:05:00'
This question already has answers here:
Getting the name of the currently executing method
(23 answers)
Closed 8 years ago.
In C++, you can use __FUNCTION_NAME__ to get the name of the function that contains __FUNCTION_NAME__.
Is there an equivalent in Java? It could, in Java, be possible to do something with this and reflection. Is there something simpler though?
Thread.currentThread().getStackTrace()
will usually contain the method you’re calling it from but there are pitfalls
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/StackTraceElement.html
Thread.currentThread().getStackTrace()[ste.length - 1 - depth].getMethodName();
depth = 0 (zero) will give current method
also
System.out.println((new Throwable()).getStackTrace()[0].toString());
Sample output:
com.junk.Junk3.main(Junk3.java:12)