DEV Community

Discussion on: Write a program or script to find Lucky Numbers

Collapse
 
kspeakman profile image
Kasey Speakman • Edited

So I pretty much went back and inlined everything in the F# solution and updated my post. Got the time way down. It should very nearly read like especially bad C code if you want to see how it does ported to C.

Your Haskell implementation is quite impressive in performance and terseness.

Collapse
 
heikodudzus profile image
Heiko Dudzus • Edited

Great to hear that. I'm afraid I can't fully claim credit for the performance. That seems an achievement of the author of the vector library (Roman Leshchinskiy) and the authors of the Glasgow Haskell Compiler. ;-)

You sped up your program, so I let the compiler inline deleteEveryNthunsing the compiler pragma {-# INLINE deleteEveryNth #-}

I had a run in 469 ms CPU time. :-D (Of course, it all depends on hardware. But I also wanted to do the next step and profit of inlining.)

I need some more time to go through your code to understand and port it to C. Great.

Thread Thread
 
kspeakman profile image
Kasey Speakman

I added a lot of comments. Hopefully that will help with a C translation. I just noticed you are starting with odds whereas I am filtering them out at cost of extra CPU time. I'll update my version tonight to do that also.

I'm still quite in awe of the Haskell version. It is quite expressive while performing the same. I don't understand all the specific functions or operators, but I "get" it in a general sense. Whereas I had to remove any semblance of human thought to run mine faster.

Thread Thread
 
kspeakman profile image
Kasey Speakman

Turned out starting with odds didn't change the runtime at all really. But it led me to some shorter code that accomplished the same thing.