aspose file tools*
The moose likes Java in General and the fly likes time taken to execute function in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "time taken to execute function in java" Watch "time taken to execute function in java" New topic
Author

time taken to execute function in java

Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Hi,
Can anyone tell me what function in Java will display the time it takes to execute a procedure of a program, and maybe a link or a brief description of how it works?
Thanks in advance.


Subhash
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
There isn't one, so you need to write your own.
Before the call to your function, do getTimeMillis() and do that again afterwards. The difference is the time taken to execute the function (plus the time needed to execute getTimeMillis() of course).


42
Zkr Ryz
Ranch Hand

Joined: Jan 04, 2001
Posts: 187
Originally posted by Subhash Sriram:
Hi,
Can anyone tell me what function in Java will display the time it takes to execute a procedure of a program, and maybe a link or a brief description of how it works?
Thanks in advance.

There's no such method in java as far as I know, but you could easily measure the time substracting the start time to the end time.
public void mainMethod(){
long start = System.currentTimeMillis();
yourInterestingMethodHere();
long finish = System.currentTimeMillis();
System.out.println("Execution time = " + (finish-start) + " milliseconds );
}
[ April 13, 2004: Message edited by: Zkr Ryz ]
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
The method that is being illustrated, it won't be that accurate right? They're background threads/processes of the OS being run, also, if your application is multi-threaded, this may further inaccurate the readings you get.
Perhaps by looping the methods of interests a few times (maybe a few 1000 times) and then taking the average would give a better estimate of the time taken to execute the methods? And, not forgetting to close as many background threads/processes as possible.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
That is always the case.
The only way you can determine the theoretical time it takes to execute any function in any language is to determine the actual machine instructions that will get executed during the call and execution of the method and then add up the number of CPU cycles for each of these instructions.
If you know the speed of the CPU you will then have the execution time of the function.
And don't laugh, that's actually what we used to do back in the good old days of Assembly programming when determining why the heck our data acquisition algorithm once in a (very regular it appeared) while skipped a byte.
It's tedious, error prone (especially with languages that are further removed from the CPU than Assembly where you don't have access to the machine code) accounting work.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I recently made a little thing for work (so I shouldn't share it) that you call like this:
StatsHelper.record( method_name, elapsed_millis )
It keeps the max, min, count and average for "buckets" of time. By default it keeps the most recent 60 one-minute buckets. The bucket count and size are both configurable. There are some simple reporting methods that roll-up buckets in different ways.
This is good in a running system. You can't call a method a thousand times just to get timings, but you can get stats on a whole day's worth of real life calls if you want.
BTW: I remember being very disappointed when IBM stopped publishing the execution time for every instruction on new CPUs. But that information was used for more evil than good in COBOL. Stupid optimizations like signed dates. Sheesh.
[ April 13, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1135

Subhash,
In my opinion, your question is probably more appropriate for the Performance forum.
For your information, the term usually associated with timing of java methods is profiling, or benchmarking. In case you are unaware, a Web site that has a lot of information on profiling is JavaPerformanceTuning. I'd also like to recommend the following JavaWorld article
Good Luck,
Avi.
Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Thanks to everyone for the replies. I was told by my instructor that any programming language I use should have a method to do that, but I guess he was wrong.
I am writing two separate programs to get the n-th fibonacci #, one program works recursively, the other iteratively. We are analyzing both algorithms by finding out how much time it takes to find n. I know that the iterative is faster, but he wants a precise value.
According to him, you should not have to write anything to do this, but like I said, he must have been wrong.
Thanks again for the help.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Subhash, I believe your instructor meant a method like System.currentTimeMillis() which enables measuring execution time (by combining two calls to currentTimeMillis() and a simple calculation) but doesn't as such implement the measurement. In fact, I'm not aware of any language that would provide a built-in way to measure execution time.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
By the way, I recently wrote three slightly different implementations of the Fibonacci sequence for my article in the April edition of JavaRanch Journal. Especially "FibonacciFloatImpl.java" might be of interest to you (this approach was new to me before I started looking at what kind of solutions for the Fibonacci sequence exist).
Warren Dew
blacksmith
Ranch Hand

Joined: Mar 04, 2004
Posts: 1332
    
    2
Originally posted by Jeroen Wenting:
The only way you can determine the theoretical time it takes to execute any function in any language is to determine the actual machine instructions that will get executed during the call and execution of the method and then add up the number of CPU cycles for each of these instructions.
If you know the speed of the CPU you will then have the execution time of the function.

Actually on modern general purpose machines, this is hard to do any more, because it's too difficult to determine how likely you are to get cache misses in two or three levels of cache and exactly how long it takes the memory controller to get back to you on loads from and stores to main memory. For most applications, testing is easier and possibly more accurate these days.
In embedded systems, which are often simpler, this can sometimes still done, though. You can even do it in directly compiled languages by disassembling the generated machine code. People who write network routers (not home 'routers', but the big things on the internet backbones) still care enough about performance to do this, for example.
Subhash Sriram
Greenhorn

Joined: Nov 10, 2003
Posts: 12
Originally posted by Lasse Koskela:
By the way, I recently wrote three slightly different implementations of the Fibonacci sequence for my article in the April edition of JavaRanch Journal. Especially "FibonacciFloatImpl.java" might be of interest to you (this approach was new to me before I started looking at what kind of solutions for the Fibonacci sequence exist).

Lasse,
Thank you for that link as well as to help me with the problem I am having. I am also using the formula you used in "FibonacciFloatImpl.java" by using the time it takes the computer to calculate that value vs. the time it takes to go through the algorithm, my whole program basically deals with algorithm complexity.
Also, I noticed something in your code, which I stupidly didnt do, which was define a variable called SQRT5, and I used Math.sqrt(5.0) every time it appeared in the formula. I am guessing that if I only define it once, it should reduce the time?
Thanks again for all the help.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Subhash Sriram:
Also, I noticed something in your code, which I stupidly didnt do, which was define a variable called SQRT5, and I used Math.sqrt(5.0) every time it appeared in the formula. I am guessing that if I only define it once, it should reduce the time?

I don't think it does. This kind of "optimizations" more often than not are totally useless -- using the constant simply makes the code more readable.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
This kind of "optimizations" more often than not are totally useless -- using the constant simply makes the code more readable.
Which is actually an excellent reason to do something like this, regardless of performance. As it happens though Lasse, your code seems to run about 5-10% faster with SQRT5 than it would with Math.sqrt(5). (On my machine, with a million repetitions.) Realize that Math.sqrt(5) is not recognized as a compile-time constant, thanks to the method call. A smart JVM may still figure out that it can be optimized away, but don't be too sure.


"I'm not back." - Bill Harding, Twister
 
Don't get me started about those stupid light bulbs.
 
subject: time taken to execute function in java