Page 1 of 2 12 LastLast
Results 1 to 20 of 21
Like Tree1Likes

Thread: log and exp optimisation

  1. #1
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default log and exp optimisation

    Hello,

    In my code I use Math.log() and Math.exp() a lot. I know they are quite intensive methods and they are really making my program slow. Is there any way to optimise the methods? Another library, or self-coded methods?

    Thank you.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I know they are quite intensive methods and they are really making my program slow.
    How do you know this?

    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.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,556
    Rep Power
    25

    Default

    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.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,648
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by malaguena View Post
    In my code I use Math.log() and Math.exp() a lot. I know they are quite intensive methods and they are really making my program slow. Is there any way to optimise the methods? Another library, or self-coded methods?
    Those methods are already quite optimzed; the optimization started before the IEEE754 format saw the light. What is giving you the performance problems?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default 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!

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    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.

  7. #7
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.

    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.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    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.

  9. #9
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    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?
    malaguena likes this.

  10. #10
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    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.
    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?

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    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?

  12. #12
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by dlorde View Post
    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?
    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!

  13. #13
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Also, how do you calculate your hash?
    I am not sure if I understand your question but I am not doing any calculation for the keys, I use the exact String values.

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    So your class M above doesn't have a hash() method?

  15. #15
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    So your class M above doesn't have a hash() method?
    No.

    Is it for generating a key?

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Doh!
    Sorry...I was thinking HashSet, rather than HashMap...
    It's a Monday.
    :)

  17. #17
    malaguena is offline Member
    Join Date
    Mar 2011
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Doh!
    Sorry...I was thinking HashSet, rather than HashMap...
    It's a Monday.
    :)
    Uh.. Then I can forget about it. :)

  18. #18
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Probably for the best.
    :)

  19. #19
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by Tolls View Post
    Sorry...I was thinking HashSet, rather than HashMap...
    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;
        }
    }

  20. #20
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,085
    Rep Power
    20

    Default

    Yes, but that's for the key.
    And the key is (apparently) a String, so has a valid hash method.

    I confused myself thinking in terms of a HashSet which would require the value class to have a hash method.

Page 1 of 2 12 LastLast

Similar Threads

  1. Optimisation of a java equijoin implimentation
    By int80 in forum Advanced Java
    Replies: 3
    Last Post: 04-16-2010, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •