• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Code Optimization or changing code  RSS feed

 
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have the following code that I have written but I want to optimize it if possible, or change it up a little. A friend helped me with some of the concepts behind it and I would like to see other ways it an be implemented as well. Thanks
Here is the code with the doc strings left in

 
Rancher
Posts: 786
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, Here are my thoughts:

Java has the JIT compiler and it is a fairly smart compiler, you can do all of the tricks in the world, but you are not guaranteed that it will have any impact because Java is going to optimize things behind the scenes for you and in spite of you. So the question is: do you have any bottlenecks that show in your system, are there segments that you see unexpected delays?

My best words on optimization in Java comes down to this:

Write dumb code, code that is textbook example and quality--don't try to be tricky--the compiler will usually do a much better job at optimization than you can.

If there is obvious delays in segments of your code, then work on optimization, run performance tests and etc in those sections, but unless you are seeing a problem--you are usually chasing your tail because Java is optimizing behind the scenes and your efforts may have little to no impact.

Les
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J Awesome wrote:Hi I have the following code that I have written but I want to optimize it if possible, or change it up a little.


Well, the very first question then is: Does it work? Every time and for any input? Because there's absolutely no point in changing or optimizing anything until it does.

Second question: What does "change it up a little" mean? Phrases like that are vague, and suggest that you don't have a proper goal in mind - and that's a really bad way of working. So I'd definitely define exactly what you mean by "change it up" before you alter a single line of code.

A friend helped me with some of the concepts behind it and I would like to see other ways it an be implemented as well.


OK, well there are a couple of things missing:
1. Your class extends TextProcessor, but we have no idea what it does.
2. It uses a TextReader, but we have no idea what that does either.
3. Your program appears to do an awful lot of things with "words", yet you don't have a Word class.
and also:
4. You appear to put words straight into your HashSet, which means that "hello" and "Hello" will be treated as different words. Is that what you want?


Now to "efficiency":
1. You might find that a HashMap<String, AtomicInteger> is a bit faster, since you can increment AtomicIntegers without having to create a new object every time.
2. You appear to populate all your Collections very specifically, including separate ones for longest and shortest.
If you used a List instead of a Collection, and had a Comparator that compares by word length, you could simply plough hash.keyset() straight into it and then sort it by length. Then the shortest words will be at the start and the longest ones at the end.
It may actually take longer than what you're doing, but I suspect you'll then have a more useful collection for doing other things.


Finally, going back to a Word class, here's a rudimentary one that might give you some ideas:Do you see the idea? You now have a class that:
(a) Compares with other "words" lexically.
(b) Has an equals() method that is consistent with both compareTo() and hashCode().
(c) Returns its length and a vowel count.
which are all things that you seem to need.

My version treats "Hello" and "hello" as the same word, but you don't have to. The idea is to create a class that does what YOU want a "word" to do.

HIH

Winston
 
Sheriff
Posts: 5124
138
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Two things about line 38. When you have a regex inside a loop, it is always better to "pre-compile" the regex.

The second thing is that since you are using a matcher, you might as well use find(). Whenever you see yourself writing a pattern like .*anything.*, you should probably use find().
 
Marshal
Posts: 60139
188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your version also avoids writing return twice in the same method
 
Jason Ram
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I will post the other 3 classes that I am using in my project when I get back to my desktop in an hour or two. thank you for the input so far.
 
Jason Ram
Greenhorn
Posts: 15
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are the other 3 classes I have that were supplied to me.\





 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!