Author |
Message |
turboliux
|
Posted: Sun Jan 29, 2006 3:24 pm Post subject: is there any time counting class in java? |
|
|
i m writing this paper on Sub-string searching algorithms and i want to compare them. The idea is to give each algorithms patterns of different length and see how they do in terms of time. Basicly the result would be time vs pattern length function. So does anyone know how can i count how quickly the algorithm does it's job?
btw, #of char. comparisons vs. pattern length function doesnt work, coz Karp-Rabin uses hash values and rarely compares characters. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
rizzix
|
Posted: Sun Jan 29, 2006 9:50 pm Post subject: (No subject) |
|
|
yea.
like this:
code: | long begin = System.nanoTime();
// some crazy intensive operation
long end = System.nanoTime();
System.out.println("time taken: " + (being - end)); |
|
|
|
|
|
|
Hikaru79
|
Posted: Sun Jan 29, 2006 10:23 pm Post subject: (No subject) |
|
|
If you're running Linux, you can simply use the shell command "time" to precisely count the amount of time that the execution took. This is actually a better measure since it will take into account all the overhead time that is spent before the Timer class would even be instantiated. |
|
|
|
|
|
Martin
|
Posted: Mon Jan 30, 2006 12:55 am Post subject: (No subject) |
|
|
The problem with that being that it makes java into a slow, platform dependent version of C++ |
|
|
|
|
|
turboliux
|
Posted: Mon Jan 30, 2006 10:20 am Post subject: (No subject) |
|
|
thnx rizzix
hmmm... so is it better to count the time in Linux or Windows?
explain more, why? plz. |
|
|
|
|
|
turboliux
|
Posted: Mon Jan 30, 2006 11:48 am Post subject: (No subject) |
|
|
btw, i tihnk it should be this way: code: | System.out.println("time taken: " + (end-begin)); | coz with (begin-end) i get negative value... |
|
|
|
|
|
wtd
|
Posted: Mon Jan 30, 2006 3:12 pm Post subject: (No subject) |
|
|
You should use some kind of real benchmarking utility. One that can take into consideration variations in your system. I mean, what happens if you run a test of one algorithm while your system is performing some background operation, and another when your system is just completely idling? |
|
|
|
|
|
turboliux
|
Posted: Tue Jan 31, 2006 12:32 pm Post subject: (No subject) |
|
|
you are right, but in real life when people are searching for something, either for a word in webpage, or for a file in their own directory, their PC is full of trash, PC is not defragmanted, Winamp is on, etc. etc. i mean conditions are not perfect. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Tue Jan 31, 2006 2:34 pm Post subject: (No subject) |
|
|
Yes, but that's no excuse for you not to do proper benchmarking.
It's one of those "try not... do, or do not" things. |
|
|
|
|
|
rizzix
|
Posted: Tue Jan 31, 2006 2:56 pm Post subject: (No subject) |
|
|
turboliux wrote: btw, i tihnk it should be this way: code: | System.out.println("time taken: " + (end-begin)); | coz with (begin-end) i get negative value... yes,, of course.. my mistake. |
|
|
|
|
|
turboliux
|
Posted: Tue Jan 31, 2006 2:59 pm Post subject: (No subject) |
|
|
wtd wrote: You should use some kind of real benchmarking utility....
ok but what could be that benchmarking utility? do you know any? |
|
|
|
|
|
wtd
|
Posted: Tue Jan 31, 2006 3:00 pm Post subject: (No subject) |
|
|
Honestly, no I don't. I don't benchmark things in a regular basis. However, I'm sure such utilities are available for your operating system of choice. Google is your friend. |
|
|
|
|
|
rizzix
|
Posted: Tue Jan 31, 2006 3:06 pm Post subject: (No subject) |
|
|
Netbeans come with a built-in profiler. It's supposed to be pretty good.
but the method is demonstrated above is fairly accurate, so long as you look at relative differences. its all good.
It is said that it might be best to run that section of code a couple of times and then benchmark it. Cuz that way you get the results after the hotspot optimizer has kicked in. |
|
|
|
|
|
wtd
|
Posted: Tue Jan 31, 2006 3:10 pm Post subject: (No subject) |
|
|
If you're oing to use the fairly simple method of checking times, in addition to what rizzix said, I'd run it several times. Perhaps hundreds of times and take an average. |
|
|
|
|
|
turboliux
|
Posted: Wed Feb 01, 2006 1:00 pm Post subject: (No subject) |
|
|
ok, i think i will do that, thnx guys |
|
|
|
|
|
|