Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Math.pow() issue  RSS feed

 
Mahesh Mamani
Ranch Hand
Posts: 110
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Would like to know which process of the two stated below is faster
int x = y*1000
or
int x = y*Math.pow(10,3)
I'm using the second option as the value 3 is user-inputted parameter...Is there any way 2 get this done without any performance issues...
Well, do not have any performance-related issues with this, but may arise in the future...
TIA

MSM
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mahesh Mamani:
Well, do not have any performance-related issues with this, but may arise in the future...

1st Rule of Optimization: Don't.
2nd Rule of Optimization (for experts only): Don't... yet.
3rd Rule of Optimization: When you *really* need to, profile before optimizing - the bottleneck isn't where you think it is.
:roll:
See also http://c2.com/cgi/wiki?OptimizeLater
[ May 22, 2002: Message edited by: Ilja Preuss ]
 
Mark Herschberg
Sheriff
Posts: 6037
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ilja Preuss:

1st Rule of Optimization: Don't.
2nd Rule of Optimization (for experts only): Don't... yet.
3rd Rule of Optimization: When you *really* need to, profile before optimizing - the bottleneck isn't where you think it is.
;)

Excellent, excellent advice! Well said. Mind if I quote you on that?
--Mark
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mark Herschberg:
Excellent, excellent advice! Well said. Mind if I quote you on that?

Oh, well - I stole it from http://c2.com/cgi/wiki?RulesOfOptimization, so I guess it would be ok...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Now I'm going to (mostly) pretend this code has actually been established to be an issue...
Would like to know which process of the two stated below is faster
Uhhh, probably the one that uses a constant.
I'm using the second option as the value 3 is user-inputted parameter
Offhand, getting this parameter from the user - even if it's just read from a file - will probably take far longer than calulating an integer power of ten. Are you doing this in a loop or something? If not, who cares? If yes - will the user parameter change each time through the loop? If not - factor the calculation out of the loop. (Along with getting the user input in the first place.)
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!