• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Discussion: optimizing code

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
I just discovered this place about a week ago and it seems like a great bunch of people.
I would like to submit a short program and see what you folks think about optimization of java code. The following is a short randomizing algorithm that I wrote which can be used to create passwords and such. The result is a character sequence of upper and lower case characters and numbers the length that you type into the shell. (ex. java CraigsRandomizer 50 -- would produce a sequence of 50 characters) It will tell you how many characters you chose, the processing time in milliseconds, and display the result. Just copy, paste, compile, and your off.
Here is the code:


You will probably note that you have to make the length more then 100-200 to start getting any processing time indication at all. The processing time result is relative to the amount of characters that are generated which quickly meet the specification.
The program uses Math.random() and creates an array of "len" characters which meet the specification.
Steps I took to optimize the code:
1) The class and method are final to maximize compilation.
2) Short circuit comparison operators are used and the comparisons listed from the largest group to the smallest group for fastest rejection.
3) An array of primitives is used and discarded for quick access and memory size.
4) Math.random() * 122 limits the result to numbers which do not exceed our needs.
Please disregard the three lines of code which are for displaying the timing and print the messages to the screen. They are just for the purpose of this discussion.
Also I am aware of the opinion that the java.lang.Math.random() does display certain consistencies and that other algorithms can produce more random results. For this purpose it is certainly random enough.
Are there any further ways to optimize this?
What about the two casts?
Would a different algorithm be faster?
Is there a more efficient way to handle memory?
Thanks for your time,
Best Regards,
Craig

(fixed formating)
[This message has been edited by Craig O'Brien (edited March 01, 2001).]
 
Ranch Hand
Posts: 156
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Two suggestions:
1. Instead of using Math.random(), which produces a double that has to be cast, you could use the following:

2. You could also eliminate the char casting by using a different string constructor for the result:
 
Craig O'Brien
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey thanks for responding Angela.
Replacing
int n = (int)(Math.random() * 122);
with
int n = rand.nextInt(122);
did yield a 1.0107% increase in performance.
Unfortunately for the result to be a character sequence the integers still need to be cast to characters so the String() constructor does not fill the need.
I see that I probably placed this in the wrong forum, I told you I was new here. I see that there is a performance forum just a few lines above. Guess I'll mosey over there.
Thanks,
Craig
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You know we have a whole forum dedicated to performance issues. The folks over there would really like this kind of a conversation.
 
Craig O'Brien
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
duhhhh...I see now.
If you could transfer me It would be great. Otherwise I'll make it over there soon......
but: crossposting is spam :0)
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're outta here dude. See you in "Perfomance" ...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic