Java Function Challenge

Fastest, smallest function wins!

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »

100 Replies - 53782 Views - Last Post: 21 August 2014 - 05:26 AM

#31 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3194
  • View blog
  • Posts: 19,220
  • Joined: 14-September 07

Re: Java Function Challenge

Posted 22 December 2010 - 10:05 AM

Why not? The judgment comes from looking at time stamps based off of your target machine. If it runs on the JVM it should be fair game, no?
Was This Post Helpful? 0
  • +
  • -

#32 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Function Challenge

Posted 22 December 2010 - 10:11 AM

Well, I suppose. We will have to re-time all of them anyway on the same machine so that we have consistent processor speeds. I have an excel spreadsheet set up for those who want to time them on January 5th.
Was This Post Helpful? 0
  • +
  • -

#33 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Java Function Challenge

Posted 22 December 2010 - 10:13 AM

View PostKYA, on 22 December 2010 - 04:02 PM, said:

136k+! I thought Clojure was supposed to be fast. ;)


It is if you *really* want it to be. There is a lot of trickery you can do to make things faster. It is possible to make Clojure generate the same bytecode as Java. I never even called out to Java in any of my examples. I'm horrified of ugly code. I might chat with some more performance savvy Clojurers and see if they can come up with a faster solution. Nonetheless, that still isn't *that* slow.


View PostDogstopper, on 22 December 2010 - 04:03 PM, said:

I hope you realize you can't win because I can't check for consistency with the others. However, it is certainly an interesting program AND it does run on the JVM. It certainly was cool to see!

Thanks!


Quote

Don't take this as a serious submission to the challenge. I just did it for fun because I was bored. Hope you enjoy it! :>

Was This Post Helpful? 0
  • +
  • -

#34 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Function Challenge

Posted 22 December 2010 - 10:16 AM

Raynes, I think I can make an exception for JVM languages...
Was This Post Helpful? 0
  • +
  • -

#35 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3194
  • View blog
  • Posts: 19,220
  • Joined: 14-September 07

Re: Java Function Challenge

Posted 22 December 2010 - 10:19 AM

So JNI is out of the question? :innocent:
Was This Post Helpful? 0
  • +
  • -

#36 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Function Challenge

Posted 22 December 2010 - 10:22 AM

View PostKYA, on 22 December 2010 - 11:19 AM, said:

So JNI is out of the question? :innocent:


Isn't JNI slow to call from Java anyway?
Was This Post Helpful? 0
  • +
  • -

#37 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3194
  • View blog
  • Posts: 19,220
  • Joined: 14-September 07

Re: Java Function Challenge

Posted 22 December 2010 - 10:29 AM

I've never actually used it. :lol:


I'm sure whatever gain I would get from writing the function in C++ would be wiped out by the interface overhead.
Was This Post Helpful? 0
  • +
  • -

#38 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2386
  • View blog
  • Posts: 5,008
  • Joined: 11-December 07

Re: Java Function Challenge

Posted 22 December 2010 - 10:34 AM

This is a tricky challenge... for the judges! Java is not that easy to measure. I just wrote two of my own methods and tried to compare them side by side. One loop doing the first 1000 times, the next loop doing the second method 1000 times. The second algorithm was twice as fast.

Then I swapped the order so the second method was run first. This time the other method was twice as fast. Looks like the first one I tested took a hit waiting for the JIT to catch up while the second method (which had a lot of similar method calls) was already mostly optimised.

Measuring the bytecode interpreter without JIT compiling might give more interesting (if slower) results!
Was This Post Helpful? 0
  • +
  • -

#39 moopet  Icon User is offline

  • binary decision maker
  • member icon

Reputation: 343
  • View blog
  • Posts: 1,189
  • Joined: 02-April 09

Re: Java Function Challenge

Posted 22 December 2010 - 11:14 AM

View PostDogstopper, on 22 December 2010 - 02:09 PM, said:

I shaved 100ns off as well by using a char array and a saved length variable:


That was the only improvement I could think of too.

I think this is one of those questions where the optimal solution is the obvious one. It's a good idea though and I'd like to see more topics started with similar challenges, just more complex ones that result in a wider range of attacks. In all languages, actually.
Was This Post Helpful? 0
  • +
  • -

#40 Raynes  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 614
  • View blog
  • Posts: 2,815
  • Joined: 05-January 09

Re: Java Function Challenge

Posted 22 December 2010 - 11:16 AM

So, I talked with a guy who is smarter than me, and here is what we came up with:

(defn count-num-chars [^String s]
  (let [len (.length s)
        space (int 32)]
    (loop [i (int 0), c (int 0)]
      (if (< i len)
        (recur
         (unchecked-inc i)
         (if (== (int (.charAt s i)) space)
           c
           (unchecked-inc c)))
        c))))



While being some of the ugliest and nasty code I've ever had to misfortune to look at, here is the output:

Quote

[email protected]:~$ cake run ~/challenge.clj
Chars outputted: 460
Time (in nanoseconds): 5608.187


Of course, this isn't my 'submission' if my code is actually being submitted, but still. Proof that Clojure can be blazing if you really, really, really want it to. I prefer "fast enough" over blazing when blazing is the result of code like this. :P

This post has been edited by Raynes: 22 December 2010 - 11:30 AM

Was This Post Helpful? 0
  • +
  • -

#41 Nikhil_07n  Icon User is offline

  • The cheese stands alone..
  • member icon

Reputation: 54
  • View blog
  • Posts: 2,490
  • Joined: 09-January 09

Re: Java Function Challenge

Posted 22 December 2010 - 11:36 AM

Edit : Stupidest post in the thread

Here's mine.

"Reverse Approach"
Spoiler



Quote

Chars outputted: 460
Time (in nanoseconds): 1358.305


Wait, am I the fastest? :unsure:

This post has been edited by Nikhil_07n: 22 December 2010 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

#42 CodeGrappler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 41
  • View blog
  • Posts: 120
  • Joined: 29-November 10

Re: Java Function Challenge

Posted 22 December 2010 - 11:41 AM

View PostNikhil_07n, on 22 December 2010 - 10:36 AM, said:

Here's mine.

"Reverse Approach"
Spoiler



Quote

Chars outputted: 460
Time (in nanoseconds): 1358.305


Wait, am I the fastest? :unsure:


Yours shouldn't work properly. And after running your solution I see that it does not.
Chars outputted: 475
Time (in nanoseconds): 6732.892

You are changing your iteration length every iteration...so it never goes through the entire string.

How you got it to output 460 is beyond me unless it is a fabricated number or you modified the string that is being searched.

This post has been edited by CodeGrappler: 22 December 2010 - 11:43 AM

Was This Post Helpful? 1
  • +
  • -

#43 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon

Reputation: 2965
  • View blog
  • Posts: 11,222
  • Joined: 15-July 08

Re: Java Function Challenge

Posted 22 December 2010 - 12:05 PM

View PostNikhil_07n, on 22 December 2010 - 12:36 PM, said:

Here's mine.

"Reverse Approach"
Spoiler



Quote

Chars outputted: 460
Time (in nanoseconds): 1358.305


Wait, am I the fastest? :unsure:


You used a method VERY similar to the way I approached mine, but CodeGrappler is correct. You need another variable for the length.
Was This Post Helpful? 1
  • +
  • -

#44 Guest_redhatter*


Reputation:

Re: Java Function Challenge

Posted 22 December 2010 - 12:14 PM

The challenge specifies fastest AND smallest... how will this combination be evaluated? Mine might be faster, but it could be 100+ more bytes than the second fastest?
Was This Post Helpful? 0

#45 KYA  Icon User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3194
  • View blog
  • Posts: 19,220
  • Joined: 14-September 07

Re: Java Function Challenge

Posted 22 December 2010 - 12:27 PM

If I'm not mistaken, size is referring to LOC rather then actual file size. In that case, I can see how other JVM languages might pose a possible judging problem.
Was This Post Helpful? 0
  • +
  • -

  • (7 Pages)
  • +
  • 1
  • 2
  • 3
  • 4
  • 5
  • Last »