• Post Reply Bookmark Topic Watch Topic
  • New Topic

time spent calculating Java  RSS feed

 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to write a method that will print out how long it takes java to calculate the sum of the squares of the numbers between 0 and x(being any given amount)?
I guess what I'm asking is how do I get java to print how long it took it to calculate the problem I gave it for the most part.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The loop is because most things you want to measure are too fast to measure only one execution.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why did you use underscores for 1_000_000?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:why did you use underscores for 1_000_000?

Using underscores was added in Java 7. They are ignored by the compiler so their only purpose is to make numeric constants more readable to humans. It doesn't matter what position(s) the underscores are inserted into, so for instance, a social security number could look like: 123_45_6789.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of the millisecond time, consider this method.
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my experience using nanoseconds is not very useful. Benchmarks don't usually have nanosecond precision, and any problem where the difference between milliseconds and nanosecond precision is significant require more advanced profiling tools than just calling nanoTime().
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:It doesn't matter what position(s) the underscores are inserted into

One note. Carey probably meant that position of underscore does not change numeric value. But you can't add underscore anywhere, there are rules for that.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okays so (i) assume (i) take say for instance (i) and square it.The (i) will loop through 100_000_000 num of times. With each loop (i) will be the next number in line and each time it will print out the time it took to calculate the mathematic equation. Just wanting to make sure (i) understand what is going on in this piece of code?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
each time it will print out the time it took to calculate the mathematic equation

No, it's more like a stopwatch.  You start the stopwatch by getting the time in millisecond before you calculate anything.  Then you calculate a million times.  Then you stop the stopwatch by taking the time again.  start minus stop is the amount of time it took to execute a million times.  Divide that by a million to get the average time one execution took.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to be clear, 'i' is an arbitrary loop counter and should probably not be part of your calculation. You could use it if you need but you'll want to be careful that you don't interfere with your loop counting or you'll get an incorrect time result.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i used this to get the square of the number.

and it gives me a time of 2.0e-6.
Alo if I wanted it to give me the time it takes to complete the equation in seconds would I change milliseconds to seconds?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are 1,000 milliseconds in a second, so you'd need to divide the result further by 1,000. Seeing as how the print is already dividing the milliseconds by a million, you could just add another 1,000 to that, giving you...

Also, it is unreliable to expect an accurate time for such a trivial operation. As you can see you're already getting an extremely low time, 2.0e-6, or 0.0000002 milliseconds or 0.0000000002 seconds.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if i add the extra 1000 it gives me 2.0E-9 .
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Stallard wrote:if i add the extra 1000 it gives me 2.0E-9 .

...and that is the correct number of seconds.
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For such a trivial calculation you may want to bump up the loop iterations from a million to a trillion, but honestly it will never give you accurate results for something so trivial.

Is that really the calculation you wanted to time? What purpose would knowing the timing serve?
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Note that your program will get optimized in two stages, once during compilation and potentially again when running in the JVM. If your loop is like the one above then the results in your variables x and y are never used so it might potentially do away with the calculation entirely.
 
Daniel Stallard
Ranch Hand
Posts: 89
Java ME MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I would have to set x to 0 outside the loop so it doesn't continue to set It back to zero every time. which I did it should also be ++x as well. But I get those same results
2.0E-9
 
Carey Brown
Saloon Keeper
Posts: 3310
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If incrementing 'x' stands on its own, then x++ and ++x are synonymous and the first form tends to be preferred. On the other hand, if you are going to increment x as part of an expression then the choice of pre or postfix is important.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!