Archive for February, 2010

Sieve of Eratosthenes in C#

In my personal quest to find out if the programming language really matters, I present to you my first piece of code.  This is my reference implementation of Sieve of Eratosthenes in my preferred language, C#.  For this and all future language implementations, I'll put the code at the top (without comments) and then narrate my experience below.  Now, to the code!

IList<int> findPrimes(int max) {
    var vals = new List<int>((int)(max/(Math.Log(max)-1.08366)));
    var maxSquareRoot = Math.Sqrt(max);
    var eliminated = new System.Collections.BitArray(max + 1);                        

    vals.Add(2);

    for (int i = 3; i <= max; i+=2) {
	if (!eliminated[i]) {
	    if (i < maxSquareRoot) {
		for (int j = i * i; j <= max; j+=2*i)
		    eliminated[j] = true;
	    }
	    vals.Add(i);
	}
    }
    return vals;
}

I started by following the wikipedia definition and then optimized from there.

Algorithm Optimizations
I cut my work in half by treating the special case of '2'. We know that 2 is prime and all even numbers thereafter are not. So, we'll add two immediately and then start looping at 3 only checking odd numbers from there forward.

After we've found a prime, we only need to eliminate numbers from it's square and forward. Let's say we want to find all prime numbers up to 100 and we've just identified 7 as a prime. Per the algorithm, I'll need to eliminate 2*7, 3*7 ,4*7, 5*7, 6*7, 7*7 ,8*7 ,9*7, 10*7 ,11*7, 12*7 ,13*7 and 14*7. None of the even multiples matter (even times an odd is always even) and none of the multiples up to the square of the prime matter since we've already done those multiples in previous loops. So really we only have to eliminate 7*7, 9*7, 11*7 and 13*7. That's a 9 fewer iterations and those savings become more fruitful the deeper you go!

The last optimization is the square root calculation and check. We know from above that we only need to start eliminating beginning at the square of the current prime. Therefore it also makes sense that we can stop even trying once we get past the to square root of the max. This saves a bunch more iterations.

Language Optimizations
Originally I had started by returning an IEnumerable<int>. I wasn't using the list you see above and instead I was using yield return i. I really like that syntax, but once I got to the VB.net version (Coming Soon!), I didn't have a direct translation for the yield keyword. I took the lazy route in the VB version and just stuffed it all into a list and returned that. To my surprise it was faster! I went back and changed the C# version above and it performed better. I'm not sure why, but I'm going with it.

What do you think that you get when do a sizeof(bool) in C#? I was surprised to find out that my trusty booleans actually take up a whole byte instead of a single bit. I speculate that there is a performance benefit that all of your types fit into a byte level offset in memory. I was thrilled to find out that we have a BitArray class that is useful for situations above when you need to store a lot of booleans and you need them to only take up a bit in memory. I'm not sure it helped anything, but I feel better knowing I'm using the least amount of memory possible. :)

Conclusion
Despite the fact that I know C# really well, I'm very thrilled that I was able to learn a few things about the language. Also, I'm really happy with the performance of this reference implementation. On my machine (2.66 GHz Core2 Duo and 2 GB of RAM) I can find all of the primes under 1,000,000 in 19ms. I think I've squeezed all I can out of this version. Please let me know if you see something I missed or did wrong and I'll make adjustments.

EDIT: I just added one more optimization that's worth noting. Instead of constructing my list with an empty constructor, I can save a several milliseconds off the larger sets by specifying a start size of the internal array structure behind the list. If I set this size at or slightly above the end count of prime numbers, then I avoid a lot of costly array copying as the array bounds keep getting hit. It turns out that there is quite a bit of math involved in accurately predicting the number of primes underneath a given number. I chose to cheat and just use Legendre's constant with the Prime Number Theorem which is close enough for my purposes. I can now calculate all primes under 1,000,000 in 10ms on my machine. Neat!

Does the Language Matter?

For the past 5 years I've been head down in C# and I really enjoy the language.  While I consider myself a C# developer primarily, there are plenty of other languages I use on a day to day basis.

  • C# - Server side code for ASP.NET websites, internal windows forms applications, and utility applications which load and export data.
  • SQL - Database querying for CRUD operations (not so much anymore, NHibernate rocks my world) and for ad-hoc reporting.
  • PL/SQL - Triggers and a few user defined functions in the database.
  • Javascript - Client side code to make things move and to retrieve data asynchronously.
  • HTML - Web page markup.
  • CSS - Web page styling and positioning.
  • Regular Expressions - Pattern matching in strings.
  • XSLT - Transforming XML documents into other outputs.
  • XPath - Querying XML documents.
  • Batch Files/Scripts - Command line programming to automate tasks, move around the file system, and do stuff to files.

There may be a few more that I'm not thinking about.  The point is that I'm not really "just a C# developer." I'm sure if you sit down and think about it, you use quite a few languages yourself. So, while I'm most comfortable in C#, I feel confident that I could bang out code in just about any language.  I'm only more comfortable with C# because it's what I've been using the most frequently as of late.

This is a personal challenge to myself.  I want to explore a few languages and see what I can learn.  The .NET (C#, VB.NET, F#) languages should be easy just because they all share the same base libraries.  Beyond that, who knows what I'll discover.

New Project: hotlinkr

(Queue cheesy TV announcer voice)

Are you a javascript developer?

Do you graciously host demos of your stuff on your own website?

Have you been wondering where your bandwidth has gone?

Do your web logs show people hotlinking to your scripts?

If you answered yes to these questions, then boy do I have a product for you. It's hotlinkr; the revolutionary javascript hotlink countermeasure that's sure to make the web a better place!

hotlinkr is targeted towards hard working open source javascript developers who provide demos of their work only to have them hotlinked from people on other sites.  You could just pull the script and have the offending site lose the functionality your script was providing. What's the fun in that? Teach them a lesson about executing code which you don't have control over.  Replace the script with hotlinkr!

hotlinkr will deface the website in one of many ways.  What you choose to do to the offending site is entirely up to you  You could be polite and just pop up a tiny alert.  You can be sneaky and overlay a div which makes nothing on the website clickable.  You could be funny and rick roll the site. Or just let the script choose at random!  The possibilities are endless! (That is, if you consider 18 possibilities endless.)

Special thanks to:

  • Jared Barboza for contributing the fail, move, spaz and vroomvroom functions as well as several IE fixes.
  • Mike Alsup for contributing the rickroll function as well as being the first to try this out on a large scale.
  • Cornify for supplying us with an unlimited number of unicorns and rainbows to distribute to the offending hotlinkers.

What are you waiting for?  Go check your logs and see who is hotlinking your javascript files right now!  Download now so you can take advantage of the special introductory price of free! That's right FREE! But wait there's more. If you download now within the next 5 minutes, we'll double the offer. That's right ladies and gentlemen, download within the next 5 minutes for free and you'll be able to download a 2nd copy for the same price as the first, FREE!

(Some restriction apply, see site for details.  Offer may apply to certain areas.  Authors are not responsible for the rage from website owners that you will inevitably piss off.  No animals were harmed in the making of this script.)