Results 1 to 20 of 21
Thread: log and exp optimisation
- 06-12-2011, 11:07 PM #1
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
- 06-13-2011, 01:28 AM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
How do you know this?I know they are quite intensive methods and they are really making my program slow.
I ask because it often remarked that "premature optimization is the root of all evil".
I doubt if you will out perform the library methods but you could use a look up table if you only need to evaluate the functions at a small number of points or only with a small amount of accuracy. All the same unless you know that speeding up these methods (rather than other aspects of your program) will make a difference it might be a waste of time.
- 06-13-2011, 01:38 AM #3
To get timings of what methods are expensive, try using the -Xprof option when you execute the program. It will print out a report showing time spent in different methods.
- 06-13-2011, 10:10 AM #4
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,375
- Blog Entries
- 7
- Rep Power
- 17
- 06-13-2011, 01:47 PM #5
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
Enumeration over a Hashtable
Thank you for the replies. After trying the -Xprof option, I could see that %75 of the running time belongs to method which has Math.log() calls,along with a Hashtable enumeration. I have noticed that if I remove the logs and just leave the Hashtable enumeration which enumerates over the items, it reduces to %60. It is still very high!
So, the problem is about the Hashtable enumeration which raises another question. Is there any other better way to enumerate over the items of Hashtable?
Thank you!
- 06-13-2011, 05:30 PM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
You are focussing on the wrong stuff.
Honestly.
Is your code running slowly?
And by that I don't mean if you get all obsessive about profile results.
Is it actually running slowly such that it's painful?
If not then simply forget about it.
- 06-13-2011, 05:52 PM #7
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
It is not easy to simply forget about it because I am not mentioning about seconds, minutes, or hours.. It is about days, even depending on the data size, weeks.
It is a machine learning code, and it has loads of iterations to train on the data. So I need to optimise my code. Otherwise, I need to wait for days to get results, which is really painful.
- 06-13-2011, 06:00 PM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
As already said, though, the things you are looking at are already optimised.
So rather than "how can I hunt through a Hashtable faster" you probably ought to be looking at "is there something that does x faster than a Hashtable".
Which would involve you telling us what "x" is.
- 06-13-2011, 06:04 PM #9
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
So, you have encountered a performance problem while running your app. You have profiled it and discovered that the major cause of this performance problem is enumeration of Hashtable, yes?
Is your application multi-threaded, or do you have some other reason for selecting a synchronized collection like Hashtable, with it's additional performance overhead, over an unsynchronized one like HashMap?
Have you tried using HashMap in place of Hashtable?
- 06-13-2011, 06:07 PM #10
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
I understand your point. The items are the objects of a class which I defined myself. The class is something like this:
class M{
String a;
Integer b;
double c;
double d;
}
I index the items with the String a. As I need to check each time if the item already exists, I need to use an indexed data structure. Are there any other better options than using a Hashtable?
- 06-13-2011, 06:10 PM #11
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
See dlorde's post.
Unless you require thread-safety then a HashMap should give you a boost, though how much I couldn't say.
Also, how do you calculate your hash?
- 06-13-2011, 06:12 PM #12
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
Yes, I have noticed that Hashtable enumeration is the problem. My application is multi-threaded. However, threads do not share the hashtables. I did not know that Hashtable is a synchronized structure. I did not have a particular reason to use a Hashtable.
I will try to use a HashMap instead.
Thank you very much for pointing at this!
- 06-13-2011, 06:16 PM #13
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
- 06-13-2011, 06:22 PM #14
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
So your class M above doesn't have a hash() method?
- 06-13-2011, 06:23 PM #15
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
- 06-13-2011, 06:23 PM #16
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Doh!
Sorry...I was thinking HashSet, rather than HashMap...
It's a Monday.
:)
- 06-13-2011, 06:25 PM #17
Member
- Join Date
- Mar 2011
- Posts
- 24
- Rep Power
- 0
- 06-13-2011, 06:31 PM #18
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Probably for the best.
:)
- 06-14-2011, 01:26 PM #19
Senior Member
- Join Date
- Jun 2008
- Posts
- 339
- Rep Power
- 5
As I understand it, provision of a hashCode method is relevant for both Hashtable and HashMap. The HashMap API docs cover performance in some detail, including this:
"This implementation provides constant-time performance for the basic operations (get and put), assuming the hash function disperses the elements properly among the buckets."
If an explicit implementation isn't provided, the default Object hashCode is used.
This is what my IDE (IntelliJ IDEA) generates for hashCode and equals for class M above:Java Code:class M { String a; Integer b; double c; double d; @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } M m = (M) o; if (Double.compare(m.c, c) != 0) { return false; } if (Double.compare(m.d, d) != 0) { return false; } if (!a.equals(m.a)) { return false; } if (!b.equals(m.b)) { return false; } return true; } @Override public int hashCode() { int result; long temp; result = a.hashCode(); result = 31 * result + b.hashCode(); temp = c != +0.0d ? Double.doubleToLongBits(c) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); temp = d != +0.0d ? Double.doubleToLongBits(d) : 0L; result = 31 * result + (int) (temp ^ (temp >>> 32)); return result; } }
- 06-14-2011, 01:42 PM #20
Moderator
- Join Date
- Apr 2009
- Posts
- 10,438
- Rep Power
- 16
Similar Threads
-
Optimisation of a java equijoin implimentation
By int80 in forum Advanced JavaReplies: 3Last Post: 04-16-2010, 05:32 AM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks