Yet another silly Python vs Java comparison

I’ve been hacking the mspsim (a simulator) source to add support for a couple of stuffs in its profiler. At a certain point I had a hash table mapping functions to how many times they were called, and I had to sort them by that number of times. How to do that?

List<entry> list = new LinkedList<entry>(callers.entrySet());
Collections.sort(list, new Comparator<entry>() {
    @Override
    public int compare(Entry o1, Entry o2) {
      return o2.getValue().compareTo(o1.getValue());
    }
});

And if it were Python:

lst = sorted(((n, fn) for fn, n in callers.iteritems()), reverse=True)

Yep, Java can be a pain.

Of course it’s not that simple, though. I’ve tested another simulator in Python and its code is very nice and readable and warm and fuzzy…

…but it’s 30 times slower than the Java one.

Leave a Reply

Your email address will not be published. Required fields are marked *